假设集合是这样的:
db.mytests.find()
{ "_id" : ObjectId("4fb277b89b8295a790efde44"),
"mylist": [
{ "foo1" :"bar1", "foo2" : "bar2" },
{"foo1" : "bar3", "foo2" : "bar4" }
],
"nonlist" : "nonlistVal" }我想在mylist中删除一个foo1等于bar1的文档,在阅读了mongodb document about updating之后,我使用了以下内容:
db.mytests.update({},{$pull:{'mylist':{'mylist.$.foo1':'bar1'}}})但它失败了。为了解决这个问题,我使用以下代码将一个新数组插入到mytests中:
db.mytests.update({},{$set:{'anotherList':[1,2,3,4]}})然后使用db.mytests.update({},{$pull:{'anotherList':{$gt:3}}})拉取数组anotherList中的元素4,成功。
我想问题出在mylist.$.foo1上?你能告诉我删除数组中文档元素的正确方法吗?
发布于 2012-05-18 00:53:13
尝试更改:
db.mytests.update({},{$pull:{'mylist':{'mylist.$.foo1':'bar1'}}})至:
db.mytests.update({},{$pull:{'mylist':{'foo1':'bar1'}}})https://stackoverflow.com/questions/10637320
复制相似问题