简体   繁体   English

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

[英]How to trim and match in a MongoDB aggregation?

I want to do a MongoDB aggregation which first matches with some values and then groups them.我想做一个 MongoDB 聚合,它首先与一些值匹配,然后对它们进行分组。 But the field which has to be matched has values in the DB which have leading and trailing spaces in some of them.但是必须匹配的字段在数据库中具有值,其中一些值具有前导和尾随空格。 I want to first trim and then match them.我想先修剪然后匹配它们。 How do I do that?我怎么做?

async AggregateTypes(_, { retailer_type }) {
  const matchCond = {};
  if(retailer_type) matchCond.cidm_retailer_type = {$in: retailer_type}
  res = await collection.aggregate([
    { 
        $match: matchCond 
    },
  ]);
}

You can try $trim operator in $expr expression match condition,您可以在$expr表达式匹配条件下尝试$trim运算符,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM