为什么会发生这种情况?对于这种差异有合乎逻辑的解释吗?
例如,我有一个数据库结构;
{
id: "1"
category: {
name: "name1"
groups: [
{
groupName : "groupName1"
title: ""
},
{
groupName : "groupName2"
title: ""
}
]
}
}
查询如下;
db.collection.aggregate({$unwind:"$category.groups"},
{$match:{"category.groups.groupName": "groupName2",
"category.name" : "name1"}})
在 mongo shell 中,它返回为;
{
id: "1"
category: {
name: "name1"
groups: [
groupName : "groupName2"
title: ""
]
}
}
在node.js中查询;
db.collection.aggregate({$unwind:"$category.groups"},
{$match:{"category.groups.groupName": "groupName2",
"category.name" : "name1"}}).
toArray(function(err, result) {
if (result) {
debugger;
var res = result;
}
});
};
node.js 结果类似于;
{
id: "1"
category: {
name: "name1"
groups: [
{
groupName : "groupName1"
title: ""
},
{
groupName : "groupName2"
title: ""
}
]
}
}
最佳答案
使用node.js驱动程序,您需要传递aggregate
管道作为数组,而不是作为单独的参数。
所以应该是:
db.collection.aggregate([{$unwind: "$category.groups"},
{$match: {"category.groups.groupName": "groupName2",
"category.name": "name1"}}
]).toArray(function(err, result) { ... });
shell version更宽容,但为了安全起见,您应该始终使用数组,否则不能包含 options
参数。
关于node.js - mongodb shell 和 node.js 中的相同查询表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36357716/