How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one mongodb mongodb

How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one


I solved it with my own function. If you want to update specified field in document you need to address it clearly.

Example:

{    _id : ...,    some_key: {         param1 : "val1",        param2 : "val2",        param3 : "val3"    }}

If you want to update param2 only, it's wrong to do:

db.collection.update(  { _id:...} , { $set: { some_key : new_info  } }  //WRONG

You must use:

db.collection.update(  { _id:...} , { $set: { some_key.param2 : new_info  } } 

So i wrote a function something like that:

function _update($id, $data, $options=array()){    $temp = array();    foreach($data as $key => $value)    {        $temp["some_key.".$key] = $value;    }     $collection->update(        array('_id' => $id),        array('$set' => $temp)    );}_update('1', array('param2' => 'some data'));


If I understand the question correctly, you want to update a document with the contents of another document, but only the fields that are not already present, and completely ignore the fields that are already set (even if to another value).

There is no way to do that in a single command.

You have to query the document first, figure out what you want to $set and then update it (using the old values as a matching filter to make sure you don't get concurrent updates in between).


Another reading of your question would be that you are happy with $set, but do not want to explicitly set all fields. How would you pass in the data then?

You know you can do the following:

db.collection.update(  { _id:...} , { $set: someObjectWithNewData } 


You can use dot-notation to access and set fields deep inside objects, without affecting the other properties of those objects.

Given the object you specified above:

> db.test.insert({"id": "test_object", "some_key": {"param1": "val1", "param2": "val2", "param3": "val3"}})WriteResult({ "nInserted" : 1 })

We can update just some_key.param2 and some_key.param3:

> db.test.findAndModify({... query: {"id": "test_object"},... update: {"$set": {"some_key.param2": "val2_new", "some_key.param3": "val3_new"}},... new: true... }){    "_id" : ObjectId("56476e04e5f19d86ece5b81d"),    "id" : "test_object",    "some_key" : {        "param1" : "val1",        "param2" : "val2_new",        "param3" : "val3_new"    }}

You can delve as deep as you like. This is also useful for adding new properties to an object without affecting the existing ones.