Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MongoDB聚合中修剪和匹配?_Mongodb - Fatal编程技术网

如何在MongoDB聚合中修剪和匹配?

如何在MongoDB聚合中修剪和匹配?,mongodb,Mongodb,我想做一个MongoDB聚合,它首先匹配一些值,然后对它们进行分组。但是必须匹配的字段在DB中有一些值,其中一些值有前导空格和尾随空格。我想先修剪一下,然后再把它们搭配起来。我该怎么做 async AggregateTypes(_, { retailer_type }) { const matchCond = {}; if(retailer_type) matchCond.cidm_retailer_type = {$in: retailer_type} res = await co

我想做一个MongoDB聚合,它首先匹配一些值,然后对它们进行分组。但是必须匹配的字段在DB中有一些值,其中一些值有前导空格和尾随空格。我想先修剪一下,然后再把它们搭配起来。我该怎么做

async AggregateTypes(_, { retailer_type }) {
  const matchCond = {};
  if(retailer_type) matchCond.cidm_retailer_type = {$in: retailer_type}
  res = await collection.aggregate([
    { 
        $match: matchCond 
    },
  ]);
}
您可以在
$expr
表达式匹配条件下尝试运算符

if(retailer_type) {
  matchCond = { 
    $expr: { 
      $in: [
        { $trim: { input: "$cidm_retailer_type" } }, 
        retailer_type
      ] 
    }
  }
}

我可以将这些匹配与使用$in运算符的其他匹配进行配对吗?可以,但是对于这种情况,对于直接匹配,可以将条件置于$expr之外。。如果我出了问题,请举例说明,这样可以给出更准确的解决方案。我检查过了,结果是正确的。谢谢