Search code examples
mongodbmongodb-querymongodb-update

Updating an attribute in MongoDB


I am trying to update an attribute of a field.

For example, updating the name "Orchid garden" to a new name, let's say, "The Beautiful Garden"...

I am following the syntax given in the MongoDB Documentation but it seems that I am missing something.

(The database is called plants.)

The field information is written in JSON, click here to preview

Here is what I was trying:

db.gardens.update( {name: "Orchid Garden"}, {$set: {name : "Gothenburg Orchid Garden"}}  )

Solution

  • You have a nested garden object so you have to use the dot notation

    db.gardens.update( {"garden.name": "Orchid Garden"}, {$set: {"garden.name" : "Gothenburg Orchid Garden"}} )