Deep Record Update in MongoDb

I have this entry in MongoDb and I use my own API:

{
    "_children" : {
        "addressesR" : [
            {
                "id" : ObjectId("530eea01071bd1a53065c1a6"),
                "personId" : ObjectId("530eea01071bd1a53065c1a4"),
                "street" : "ivermey",
                "city" : "perth",
                "_children" : {
                }
            },
            {
                "_children" : {
                    "configId" : {
                        "a" : {
                            "_children" : [
                                {
                                    "b" : 10
                                },
                                {
                                    "b" : 20
                                }
                            ]
                        }
                    }
                },
                "city" : "perth",
                "configId" : ObjectId("530eea01071bd1a53065c1a3"),
                "id" : ObjectId("530eea01071bd1a53065c1a5"),
                "personId" : ObjectId("530eea01071bd1a53065c1a4"),
                "street" : "bitton"
            }
        ],
    }
}

I need to update in one request the nested "b" to 30. I can find the entry:

db.peopleR.find( { '_children.addressesR._children.configId.a._children.b': 20 } );

But it's hard for me to find a way to update this particular value.

I'm trying to:

db.peopleR.update( { '_children.addressesR._children.configId.a._children.b': 20 }, { $set: { '_children.addressesR.$._children.configId.a.$._children.b': 30 }   }  )

But I get:

Cannot apply the positional operator without a corresponding query field containing an array.

Now, given that for other restrictions I absolutely need to update only "b", is there any way to do this? Or is it possible to use $ operand twice? (i.e. can I only update the inside of an object if it is only 1 level down?)

+4
source share
2 answers

, $ . .

, , $ first. , , , , . , - :

{ $set: { '_children.addressesR.1._children.configId.a._children.$.b': 30 } 

index 1 , .

, :

{ $set: {"_children.addressesR.$._children.configId.a._children": [ { b: 10}, { b: 30} ] } }

, , index first.

, , , - . , , , -, .

+3

. $ _children. . , . , .

db.peopleR.update( { '_children.addressesR._children.configId.a._children.b': 20 }, { $set: { '_children.addressesR.$._children.configId.a._children.0.b': 30 }   }  )

, , , .

: mongodb (http://docs.mongodb.org/manual/reference/operator/update/positional/)

positional $ , , , , $placeholder - .

0

Source: https://habr.com/ru/post/1529155/


All Articles