我有mongoDB子文档结构,如下所示,
{
"basecampaign" :
[
{
"timestamp" : "2014-07-28 08:09:22",
"latest" : 0,
"campaign" :
[ {
"id" : "1",
"name" : "status camp 1"
} ]
},
{
"timestamp" : "2014-07-29 08:09:22",
"latest" : 0,
"campaign" :
[ {
"id" : "2",
"name" : "status camp 2"
} ]
},
{
"timestamp" : "2014-07-30 08:09:22",
"latest" : 1,
"campaign" :
[ {
"id" : "3",
"name" : "status camp 3"
} ]
}
]
}
我想使用id删除子文档,我想删除id = 3
的块{
"timestamp" : "2014-07-30 08:09:22",
"latest" : 1,
"campaign" :
[ {
"id" : "3",
"name" : "status camp 3"
} ]
}
我怎么能实现这个建议呢?
答案 0 :(得分:1)
您可以使用$pull
。例如:
db.collection.update({},
{$pull:{basecampaign: {campaign.id:3}}},
{multi: true})
从所有文档中删除basecampaign
campaign
个id
文档。
或者,如果您只想删除广告系列块(不明白您的问题)
db.collection.update({parent document select},
{$pull:{basecampaign.campaign: {id:3}}})
从所选部分文档中的所有basecampaign文档中删除ID为3的广告系列块。