根据条件删除文档

时间:2017-02-06 14:30:30

标签: node.js mongodb

假设我有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}},并且不会因查询而获得任何内容。

有可能实现这个目标吗?

0 个答案:

没有答案