Reputation: 985
I have docs like
{"_id":100, "age": {"bob": 20, "jack": 30}}
and I only want to update existing person with their ages,but if I update a non-existent user:
db.products.update({ _id: 100 }, {$set: { "age.tom": 10 }})
,
mongo will create "tom" and set the age which is not my expectation.
Related doc: https://docs.mongodb.com/manual/tutorial/update-documents/
If you specify a dotted path for a non-existent field, $set will create the embedded documents as needed to fulfill the dotted path to the field.
Any way to do the update while not upsert
for a sub-field?
Upvotes: 1
Views: 226
Reputation: 1888
db.example.update({ "age.tom":{$exists:true} },{ $set: { "age.tom": 10 }})
if you want "_id"
as a filter then ,
db.example.update({"_id" : 100, "age.tom":{$exists:true} },{ $set: { "age.tom": 10 }})
Upvotes: 2