在MongoDb中查找完全匹配的数组或具有数组的所有值
我有这样的收款记录 [ { shape : [{id:1,status:true},{id:2,status:false}] }, { shape : [{id:1,status:true}] } ] 我想获取与数组完全匹配的数据,即包含所有元素。数组的一部分 例如,其中shape.id=[1,2]/[{id:[1,2]}]任何一个都是首选 那么它应该只返回 [ { shape : [{id:1,status:true},{id:2,status:false}] } ] 因此,如果有任何本机mongodb查询,请帮助我 谢谢在MongoDb中查找完全匹配的数组或具有数组的所有值,mongodb,mongoose,mongodb-query,sails-mongo,Mongodb,Mongoose,Mongodb Query,Sails Mongo,我有这样的收款记录 [ { shape : [{id:1,status:true},{id:2,status:false}] }, { shape : [{id:1,status:true}] } ] 我想获取与数组完全匹配的数据,即包含所有元素。数组的一部分 例如,其中shape.id=[1,2]/[{id:[1,2]}]任何一个都是首选 那么它应该只返回 [ { shape : [{id:1,status:true},{id:2,status:false}]
-ND如果mongo文件如下
{
"_id" : ObjectId("54eeb68c8716ec70106ee33b"),
"shapeSize" : [
{
"shape" : [
{
"id" : 1,
"status" : true
},
{
"id" : 2,
"status" : false
}
]
},
{
"shape" : [
{
"id" : 1,
"status" : true
}
]
}
]
}
然后使用下面的聚合来匹配条件
db.collectionName.aggregate({
"$unwind": "$shapeSize"
}, {
"$match": {
"$and": [{
"shapeSize.shape.id": 2
}, {
"shapeSize.shape.id": 1
}]
}
}, {
"$project": {
"_id": 0,
"shape": "$shapeSize.shape"
}
})
这里是更简单的查询
db.shapes.find({'shape.id':{$all:[1,2]},shape:{$size:2}});
您需要使用$elemMatch运算符: