I have a database of users like in the example below and I need to update the city of the user from Bonn to Berlin.
{
"_id" : "Louis",
"registered" : true,
"likes" : [
"tennis",
"cooking"
],
"addr" : {
"city" : "Bonn",
"country" : "Germany"
}
}
I have tried the following but it does not seem to work. Any ideas?
db.users.update( {_id:"Louis"}, {$set:{city:"Berlin"}} )
1 Answer 1
You should have quotes around field names in sub-documents
db.test.update( {_id : "Louis"}, {$set : {"addr.city" : "Berlin"}})
answered Dec 21, 2013 at 12:56
bsd
2,7371 gold badge19 silver badges24 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
jjmartinez
The answer is uncompleted if you don't say that to access to a field of a subdocument you need to use the dot notation. The question don't have the dot notation in the code.
lang-js