简体   繁体   中英

Update a couple of values in nested array MongoDB

I would like to update a couple of elements in they match given values: Example : I have a collection with this structure:

 {
 "_id" : ObjectId("52936a0270c68c04063f0300"),
  "channel" : "1",
  "content" : "145548",
  "keywordsValues" : [
                        [ObjectId("52816d3370c68c2c1c4b0500"), ObjectId("52816d3370c68c2c1c9f0500")],
                        [ObjectId("52816d3370c68c2c1c510500"), ObjectId("52816d3370c68c2c1c890500")],
                        [ObjectId("52816d3370c68c2c1c550500"), ObjectId("52816d3370c68c2c1c850500")],
                        [ObjectId("52816d3370c68c2c1c6b0500"), ObjectId("52816d3370c68c2c1c990500")]
                    ]
}

And I need to update "keywordsValues" field (just one of the couple in the array [])

Something for example update the 2nd element in keywordsValues: If we found [firstId,secondId] in keywordsValues field then update the second secondId

$database->Measurements->update(
array('keywordsValues'=>
array(new MongoId(52816d3370c68c2c1c4b0500),new MongoId(52816d3370c68c2c1c9f0500)',$atomic'=>'true')),
array('$set'=>array('keywordsValue.$'=>new MongoId($idNewValue))));

But this doesn't work...

try
array('$set'=>array('keywordsValue'=>new MongoId($idNewValue))));


instead of

array('$set'=>array('keywordsValue.$'=>new MongoId($idNewValue))));


i hope it help to you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM