我有以下文件的数据库:
> 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 })
所以这里的问题似乎与我正在处理嵌入式对象的事实有关。
感谢您的帮助。
谢谢!
答案 0 :(得分:6)
$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"] } }
}
)
同样的事情,但只是" longhand"形式。