Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update value of specific embedded document, inside an array, of a specific document in MongoDB?

I have the following structure in my document:

{
  _id : ObjectId("43jh4j343j4j"), 
  array : [
            { 
              _arrayId : ObjectId("dsd87dsa9d87s9d7"),
              someField : "something",
              someField2 : "something2"
            },
            { 
              _arrayId : ObjectId("sds9a0d9da0d9sa0"),
              someField : "somethingElse",
              someField2 : "somethingElse2"
            }
   ]
 }

I want to update someField and someField2 but only for one of the items in the array, the one that matches _arrayId (e.g. _arrayId : ObjectId("dsd87dsa9d87s9d7"); and only for this document (e.g. _id : ObjectId("43jh4j343j4j") ) and no other.

The arrayIds are not unique to the document that's why I need it to be for a specific document. I could use the $ positional operator if I wanted to update that value within the array for every document it exists in, but that's not what I want.

I am trying to accomplish this in java but a command line solution would work as well.

like image 825
rjgonzo Avatar asked Sep 15 '11 04:09

rjgonzo


People also ask

How do you update an element in an array in MongoDB?

To update a set of elements matching certain filters, we must use the filtered positional operator $[<identifier>] where <identifier> is a placeholder for a value that represents a single element of the array. We must then use the third parameter (options) of the updateMany method to specify a set of arrayFilters .

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades.

How do you get a specific object from an array in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.


2 Answers

Here is RameshVel's solution translated to java:

    DB db = conn.getDB( "yourDB" ); 
    DBCollection coll = db.getCollection( "yourCollection" );

    ObjectId _id = new ObjectId("4e71b07ff391f2b283be2f95");
    ObjectId arrayId = new ObjectId("4e639a918dca838d4575979c");

    BasicDBObject query = new BasicDBObject();
    query.put("_id", _id);
    query.put("array._arrayId", arrayId);

    BasicDBObject data = new BasicDBObject();
    data.put("array.$.someField", "updated");

    BasicDBObject command = new BasicDBObject();
    command.put("$set", data);

    coll.update(query, command);
like image 144
rjgonzo Avatar answered Sep 21 '22 23:09

rjgonzo


You could still use $ positional operator to accomplish this. But you need to specify the objectid of the parent doc along with the _arrayid filter. The below command line query works fine

db.so.update({_id:ObjectId("4e719eb07f1d878c5cf7333c"),
              "array._arrayId":ObjectId("dsd87dsa9d87s9d7")},
              {$set:{"array.$.someField":"updated"}})
like image 30
RameshVel Avatar answered Sep 24 '22 23:09

RameshVel