我在mongo db中有一个以下结构
{
"_id" : ObjectId("58e8d3323fe482ef368b4567"),
"productId" : "poe10001134",
"batchdetails" : [
{
"batchidId" : "BD0166",
"batchno" : "BbfN-1444-SBRPX"
},
{
"batchidId" : "BD0167",
"batchno" : "Bff-8444-kfkff"
},
{
"batchidId" : "BD01447",
"batchno" : "Bff-8415-kfkff"
}
]
}
我想实现以下结果
"的productId" :" poe10001134"
" batchno" :" Bff-8415-kfkff"
当我执行以下查询
时db.getCollection('table').findOne( {"batchdetails.batchidId" : "BD01447"},{productId:1,"batchidId.batchno":1})
它返回所有批次没有
"的productId" :" poe10001134"
" batchno" :" BbfN-1444-SBRPX"
" batchno" :" Bff-8444-kfkff"
" batchno" :" Bff-8415-kfkff"
答案 0 :(得分:1)
这样做:
db.getCollection('table').findOne( {"batchdetails.batchidId" : "BD01447"},
{productId:1,"batchidId.batchno.$":1})
投影中的$
包含查询中匹配的batchno
数组元素的索引。