如何在MongoDB聚合中修剪和匹配?
我想做一个MongoDB聚合,它首先匹配一些值,然后对它们进行分组。但是必须匹配的字段在DB中有一些值,其中一些值有前导空格和尾随空格。我想先修剪一下,然后再把它们搭配起来。我该怎么做如何在MongoDB聚合中修剪和匹配?,mongodb,Mongodb,我想做一个MongoDB聚合,它首先匹配一些值,然后对它们进行分组。但是必须匹配的字段在DB中有一些值,其中一些值有前导空格和尾随空格。我想先修剪一下,然后再把它们搭配起来。我该怎么做 async AggregateTypes(_, { retailer_type }) { const matchCond = {}; if(retailer_type) matchCond.cidm_retailer_type = {$in: retailer_type} res = await co
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之外。。如果我出了问题,请举例说明,这样可以给出更准确的解决方案。我检查过了,结果是正确的。谢谢