我有以下文件的数据库:
> 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"
}
]
}
我正在尝试编写查询以从奖励数组中删除“斯德哥尔摩”或“好莱坞”颁发的奖励对象,但以下查询不起作用:
> 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
运算符实际上是一个“特殊情况”快捷方式,它适用于其中只有值的数组,例如您的备用情况。
您真正想要的是 $pull
,它的参数是对数组中包含的文档的“查询”。因此,您的列表将成为 $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"] } }
}
)
同样的事情,但只是“普通”形式。