pullAll 同时删除嵌入的对象

pullAll while removing embedded objects

我有包含以下文档的数据库:

> db.bios.find({"name.first":"James"}).pretty()
{
        "_id" : 9,
        "name" : {
                "first" : "James",
                "last" : "Gosling"
        },
        "birth" : ISODate("1955-05-19T04:00:00Z"),
        "contribs" : [
                "Java",
                "C",
                "Scala",
                "UNIX"
        ],
        "awards" : [
                {
                        "award" : "The Economist Innovation Award",
                        "year" : 2002,
                        "by" : "The Economist"
                },
                {
                        "award" : "Officer of the Order of Canada",
                        "year" : 2007,
                        "by" : "Canada"
                },
                {
                        "award" : "nobel",
                        "by" : "Stockholm"
                },
                {
                        "award" : "nobel2",
                        "by" : "Stockholm"
                },
                {
                        "award" : "oscar",
                        "year" : 2015,
                        "by" : "Hollywood"
                }
        ]
}

我正在尝试编写查询以从奖励数组中删除由 "Stockholm" 或 "Hollywood" 颁发的奖励对象,但以下查询不起作用:

> db.bios.update({"name.first":"James"}, {$pullAll:{"awards.by":["Stockholm","Hollywood"]}})
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 0,
        "nModified" : 0,
        "writeError" : {
                "code" : 16837,
                "errmsg" : "cannot use the part (awards of awards.by) to travers
e the element ({awards: [ { award: \"The Economist Innovation Award\", year: 200
2.0, by: \"The Economist\" }, { award: \"Officer of the Order of Canada\", year:
 2007.0, by: \"Canada\" }, { award: \"nobel\", by: \"Stockholm\" }, { award: \"n
obel2\", by: \"Stockholm\" }, { award: \"oscar\", year: 2015.0, by: \"Hollywood\
" } ]})"
        }
})
>

类似的查询适用于从 contribs 数组中删除项目:

> db.bios.update({"name.first":"James"}, {$pullAll:{"contribs":["Java","Fortran"
]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

所以这里的问题似乎与我正在处理嵌入式对象有关。

非常感谢你的帮助。

谢谢!

$pullAll 运算符实际上是一个 "special case" 快捷方式,它适用于仅包含值的数组,例如您的备用大小写。

你真正想要的是$pull and it's argument is a "query" for the documents contained in the array. So your list then becomes an argument to $in:

db.bios.update(
   { "name.first": "James" },
   { 
      "$pull": { 
         "awards": { "by": { "$in": ["Stockholm", "Hollywood"] } } 
      } 
   }
)

所以在你的另一个例子中,$pullAll 的更长形式是:

db.bios.update(
   { "name.first": "James" },
   {
       "$pull": { "contribs": { "$in": ["Java","UNIX"] } }
   }
)

同样的事情,只是 "longhand" 形式。