In the coupes
collection in my database, an example record looks like this:
db.coupes.findOne()
{
"_id" : ObjectId("5949834be916d79e033500c6"),
"rank" : 10,
"brand" : "Toyota",
"model" : "86SE"
}
However, I'd like to update it so that it looks like the following:
{
"_id" : ObjectId("5949834be916d79e033500c6"),
"rank" : 10,
"brand" : "Toyota",
"model" : "86SE",
"engineSpec" : {"cylinderConfiguration": "H-4", "transmission" : "6 speed auto"}
}
How might I accomplish this?
Simply copy the text you want repeated and paste it in the repeated location use menu sequence Edit>Paste Special. Paste as Formatted or Unformatted text and select the radio button "Paste link." This automatically creates a bookmark at the source location and creates a Link field at the destination locations.
Update all fields in a documentPress Ctrl + A. Press F9. If your document has tables with fields or formulas, you might need to select each table separately and press F9.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With