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"}} )

1 Answer 1

3

You should have quotes around field names in sub-documents

db.test.update( {_id : "Louis"}, {$set : {"addr.city" : "Berlin"}})
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.