我在 MongoDB(2.4.5) 中有以下文档
{
"_id" : 235399,
"casts" : {
"crew" : [
{
"_id" : 1186343,
"withBase" : true,
"department" : "Directing",
"job" : "Director",
"name" : "Connie Rasinski"
},
{
"_id" : 86342,
"withBase" : true
}
]
},
"likes" : 0,
"rating" : 0,
"rating_count" : 0,
"release_date" : "1955-11-11"
}
我想从 casts.crew 中的数组元素中删除 withBase 字段 ..
我试过了
db.coll.update({_id:235399},{$unset: { "casts.crew.withBase" : 1 } },false,true)
什么都没有改变。
并尝试了这个..
db.coll.update({_id:235399},{$unset: { "casts.crew" : { $elemMatch: { "withBase": 1 } } } },false,true)
它从文档中删除了整个船员数组。
有人可以向我提供正确的查询吗?
最佳答案
您可以使用新的 positional identifier
在 3.6 中更新数组中的多个元素。
类似
db.coll.update( {_id:235399}, {$unset: {"casts.crew.$[].withBase":""}} )
$[]从 crews
数组中删除所有 withBase
属性。它充当一个占位符,用于更新数组中的所有元素。
使用 multi true 来影响多个文档。
https://stackoverflow.com/questions/19945924/
相关文章:
node.js - MongoDB Node findone如何处理没有结果?
mongodb - 如何列出 mongo shell 中的所有用户?
regex - Node.js 和 Mongoose 正则表达式查询多个字段
mongodb - Cassandra、Membase、Hadoop、MongoDB、RDBMS等如
mongodb - 从现有 MongoDB 条目中删除键/值
mongodb - "db.createUser is not a function"和 "pass