0

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"}} )
asked Dec 21, 2013 at 11:27

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.