假设我有db as;
{
id: 1,
address:
[
{type: "home" , street: ""},
{type: "work" , street: ""},
{type: "school" , street: ""}
]
}
我想根据类型删除一些address
个对象。
例如,使用type: "school"
删除id : 1
我将查询写为;
db.collection.update({ "id" : 1}, {$pull : {"address" : {"type" : "school"}}}
通过这种方式,我可以删除address
字段的每个对象。
但我想知道的是;当查询删除address
的最后一个对象时,是否可以删除文档。
让我通过一个例子澄清一下;
收藏有;
{
id: 1,
address:
[
{type: "school" , street: ""}
]
}
查询是;
db.collection.update({ "id" : 1}, {$pull : {"address" : {"type" : "school"}}}
查询结果,文档成为;
{
id: 1,
address:
[
]
}
我想要的是删除带有document
的{{1}},并且不会因查询而获得任何内容。
有可能实现这个目标吗?