3

I want to store objects like this with mongodb:

{    
    field: 'example',  
    attr: {
        tmp : 'test',
        tmp1 : 'test1'
    }
};

Now I would like to search for the entry which has the property field:'example' and add a field to its attr object. How can I do this in mongojs?

user2226834
  • 63
  • 1
  • 6

1 Answers1

4

You can add the tmp2:'test2' value into the collection as follows.

db.myObject.update({field:'example'},{$set:{'attr.tmp2':'test2'}})
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • db.myObject.update({field:'example'},{$set:{'attr.tmp2':'test2'}, {upsert: true}}) <-- if tmp2 is not present in the current document. – Ryan W Kan May 10 '15 at 22:42