簡體   English   中英

mongodb:使用包含文檔和子文檔的where子句刪除子文檔

[英]mongodb: remove subdocument with where clause that includes document & subdocument

這是我的收藏(工人):

"type" : "Manager",
"employees" : [{
    "name" : "bob"
    "id" : 101
    },{
    "name" : "phil"
    "id" : 102
    },{
    "name" : "bob"
    "id" : 103
    }]

首先:這不是一個數組所以$ pullAll將不起作用或其他數組命令。 所有我想要做的是:(1)搜索的集合中的id 101 類型管理器中所有的子文檔。 (2)如果101存在於“Manager”子文檔中,我想刪除項目103。

在這個問題上,我一直在為互聯網傾倒兩天而無法理解。

我試過這個(以及許多其他變體):

db.workers.update( {"type":"Manager","employees.id":101},{$pull : {"employees.id" : {"id" : 103}}},false,true)

$pull對象的語法已關閉。 試試這個:

db.workers.update({"type":"Manager","employees.id":101},
    {$pull : {"employees" : {"id" : 103}}},false,true)

確認他們被刪除:

db.workers.find({
    type: "Manager", 
    $and: [{'employees.id': 101}, {'employees.id': 103}]
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM