Reputation: 361
Why this is happening? Is there a logical explanation for this difference?
For example I have a db structure as;
{
id: "1"
category: {
name: "name1"
groups: [
{
groupName : "groupName1"
title: ""
},
{
groupName : "groupName2"
title: ""
}
]
}
}
Query is the following;
db.collection.aggregate({$unwind:"$category.groups"},
{$match:{"category.groups.groupName": "groupName2",
"category.name" : "name1"}})
In mongo shell it returns as;
{
id: "1"
category: {
name: "name1"
groups: [
groupName : "groupName2"
title: ""
]
}
}
Query in 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;
}
});
};
where in node.js result is like;
{
id: "1"
category: {
name: "name1"
groups: [
{
groupName : "groupName1"
title: ""
},
{
groupName : "groupName2"
title: ""
}
]
}
}
Upvotes: 2
Views: 90
Reputation: 311835
With the node.js driver, you need to pass your aggregate
pipeline as an array, not as separate parameters.
So it should be:
db.collection.aggregate([{$unwind: "$category.groups"},
{$match: {"category.groups.groupName": "groupName2",
"category.name": "name1"}}
]).toArray(function(err, result) { ... });
The shell version is more forgiving, but to be safe, you should always use an array as you can't include an options
parameter otherwise.
Upvotes: 2