开发者

How to remove a reference in mongodb?

开发者 https://www.devze.com 2022-12-21 01:50 出处:网络
I have the following object { \"_id\" : ObjectId(\"4b8699aa3b97dc29dd000000\"), \"name\" : \"test\", \"email\" : \"test\",

I have the following object

   {
        "_id" : ObjectId("4b8699aa3b97dc29dd000000"),
        "name" : "test",
        "email" : "test",
        "url" : "test",
        "items" : [
                {
                        "$ref" : "item",
                        "$id" : ObjectId("4b866a043b97dc22a9000001")
      开发者_如何学编程          }
        ]}

How can I remove the reference from items? I have the objectid for the reference? I tried the following, which does work for normal list items but not with a reference.

db.foo.update(foo, {$pull: {'items': {'$id': ObjectId("4b866a043b97dc22a9000001") } } })


The problem was the version. In the old version you have to give all the keys and values to pull. After updating to version 1.3.2 this was not necessary and the code worked.


try this:

db.foo.update(foo, {$pull: {'items': {'$id': new ObjectId("4b866a043b97dc22a9000001") } } })
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号