4
在下面的记录我想item_details其中类型是“系统”和id获取子文件中的记录是“7634e9639f5c25f2434b72d8a”MongoDB的使用过滤器
var operator = {
'item_details': {
$elemMatch: {
type: 'custom'
}
}
}
var query = {
_id : "7634e9639f5c25f2434b72d8a"
};
req.db.collection('products').find(query, operator).toArray(function (err, result) {
if(err)throw err;
console.log(result);
});
它是只返回的第一个元素。但需要获取与查询和投影相匹配的所有记录。 下面是我想要查询的示例数据。
[
{
"_id": "7634e9639f5c25f2434b72d8a",
"item_details": [
{
"quantity": 1,
"sub_total": 1201,
"type": "system"
},
{
"quantity": 19,
"sub_total": 140,
"type": "custom"
},
{
"quantity": 81,
"sub_total": 130,
"type": "custom"
},
{
"quantity": 71,
"sub_total": 90,
"type": "system"
}
]
},
{
"_id": "564e9639f5c25f2434b72d8a",
"item_details": [
{
"quantity": 1,
"sub_total": 101,
"type": "system"
},
{
"quantity": 9,
"sub_total": 40,
"type": "custom"
},
{
"quantity": 8,
"sub_total": 30,
"type": "custom"
},
{
"quantity": 7,
"sub_total": 60,
"type": "system"
}
]
}
]