sadaf2605
sadaf2605

Reputation: 7540

add to nested json in mongoDB

For an instance I have a json document in mongodb

{
a:{b:c}
}

Now what am I suppost to do if I want to add to a nested json which will make it look like:

{
a:{b:c, d:e}
}

I have read the documentation of update. It has documented about $inc, $rename, $set & $unset. But I want it to add with existing elements of a. just like $addToSet do with an array.

Upvotes: 1

Views: 1076

Answers (1)

beny23
beny23

Reputation: 35018

You can use the $set operator:

collection.update({'a.b': 'c' }, {$set: {'a.d': 'e'}} )

Upvotes: 2

Related Questions