So, you should use 'update' method and pass there two objects: first one to find document which you are going to update and second one - update data.
As you see, I am using '$set' operator to prevent object from being totally replaced with the passed data.
Learn more about update method and $set operator in official docs.
db.coupes.update(
{
_id: ObjectId("5949834be916d79e033500c6")
},
{
$set : {
engineSpec : {"cylinderConfiguration": "H-4", "transmission" : "6 speed auto"} }
}
}
)
I used ObjectId as the only find parameter in my example, because it is updating single uniq document. Actually you can find objects by any fields, but remember that if you have multiple objects which have same field/value - you need to add multi:true
option to your query to update all suitable documents:
db.coupes.update(
{
rank: 10
},
{
$set : {
engineSpec : {"cylinderConfiguration": "H-4", "transmission" : "6 speed auto"} }
}
},
{
multi: true
}
)
Such code will update all documents where rank is equal to 10. Also you should notice that I added 'multi' option in third object, which you did not see before. Read more about update options here.