MongoDB聚合查询,在node.js中使用where
我在node.js中有以下mongodb查询,它为我提供了一个唯一的邮政编码列表,并计算了邮政编码在数据库中出现的次数MongoDB聚合查询,在node.js中使用where,node.js,mongodb,Node.js,Mongodb,我在node.js中有以下mongodb查询,它为我提供了一个唯一的邮政编码列表,并计算了邮政编码在数据库中出现的次数 collection.aggregate( [ { $group: { _id: "$Location.Zip", count: { $sum: 1 } } }, { $sort: { _id: 1 } }, { $match: { count: { $gt: 1 }
collection.aggregate( [
{
$group: {
_id: "$Location.Zip",
count: { $sum: 1 }
}
},
{ $sort: { _id: 1 } },
{ $match: { count: { $gt: 1 } } }
], function ( lookupErr, lookupData ) {
if (lookupErr) {
res.send(lookupErr);
return;
}
res.send(lookupData.sort());
});
});
如何修改此查询以返回一个特定的邮政编码?我尝试了condition子句,但未能使其工作。在$match pipeline操作符add中
{ $match: { count: { $gt: 1 },
_id : "10002" //replace 10002 with the zip code you want
}}
作为补充说明,您应该将$match操作符放在第一位,并且通常在聚合链中尽可能高。需要筛选结果的聚合可以使用
$match
操作符完成。在不调整现有内容的情况下,我建议只在聚合列表顶部为您想要返回的邮政编码插入一个$match
collection.aggregate( [
{
$match: {
zip: 47421
}
},
{
$group: {
...
此示例将导致$match
之后的每个聚合操作仅处理zip
键的$match
返回值47421
的数据集