我有数据需要获取设备为工作分配的批次数。我的数据样本具有以下格式
"_id" : ObjectId("5968c6aa5a44c946bc13d184"),
"listType" : "OPEN",
"resourceAssigned" : [
{
"deviceId" : "59522e01e67d9f1adcd7a158",
"capacity" : "2",
"lot" : NumberInt(1)
},
{
"deviceId" : "59522e01e67d9f1adcd7a158",
"capacity" : "7",
"lot" : NumberInt(2)
},
{
"deviceId" : "59522e01e67d9f1adcd7a158",
"capacity" : "5",
"lot" : NumberInt(3)
},
{
"deviceId" : "59522e01e67d9f1adcd7a151",
"capacity" : "6",
"lot" : NumberInt(1)
},
{
"deviceId" : "59522e01e67d9f1adcd7a151",
"capacity" : "3",
"lot" : NumberInt(2)
}
],
"materialHandlingUnit" : [
],
"__v" : NumberInt(0)
如果我需要搜索ang得到针对deviceId键的计数,那么它应该返回它在resourceAssigned数组中的次数。什么查询可以帮助我解决这个问题?
从我这边建立查询的试验是:
var pickListId = req.body.pickListId;
var deviceId = req.body.deviceId;
var pipeline = [
{"$match": {"resourceAssigned.deviceId": deviceId}},
{"$unwind": "$resourceAssigned"},
{"$match": {"resourceAssigned.deviceId": deviceId}},
{
"$group": {
"_id": '$_id',
"numberOfEntries": {"$sum": 1}
}
}
];
pickListModel.aggregate(pipeline, function (err, pickAggregateRecord) {
if (err) {
res.json({message: 'INTERNAL SERVER ERROR, UNKNOWN SERVER ERROR HAS OCCURRED!!!!!', status: 'error', statusCode: '500'});
} else {
res.json(pickAggregateRecord);
}
});
上述查询有效,但没有针对指定的' _id'进行查找的条件。我想要的查询应该作为findOne工作而不能找到。