我的文件是这样的:
// collection: photos
{
"_id" : ObjectId("5835840fb6ca90e020020b22"),
"customerIds" : [
{
"code" : "SHDR327D7DVKHBT3",
"cType" : "photoPass",
"userIds" : ['aaa','bbb']
},
{
"code" : "SHDR327D7DVKHBT3",
"cType" : "photoPass",
"userIds" : ['ddd','aaa','ccc']
}
]
}
customerIds
中的两个文件是相同的。现在我要删除所有' aaa'在userIds
之外,如何在一个命令中实现这一目标?
我使用此命令删除它们,但一次只能删除一个。
db.photos.update(
{"customerIds.code":'SHDR327D7DVKHBT3',"customerIds.userIds":'aaa'},
{$pull: {"customerIds.$.userIds":{$in:['aaa']}}},
{ multi: true,upsert: false}
)
答案 0 :(得分:0)
这就是你想要做的吗?
db.testCollection.aggregate([
{$unwind: "$customerIds"},
{$unwind:"$customerIds.userIds"},
{$match:{"customerIds.userIds":"aaa"}},
{$project:{"customerIds.userIds":1,_id:0}}
])
这将返回'aaa':
{
"_id" : ObjectId("584708e6888ff31225c2e375"),
"customerIds" : {
"userIds" : "aaa"
}
}
{
"_id" : ObjectId("584708e6888ff31225c2e375"),
"customerIds" : {
"userIds" : "aaa"
}
}