How to replace third_id in this record (only one document needs to be updated)?
> db.collection.find( {"_id" : ObjectId("4f90cf0cd4bea011930001a3"), "first_id": ObjectId("4edf056800126757c000000f")}) { "second_id" : ObjectId("4f355e430012671d77000ec0"), "third_id" : ObjectId("4edf056800126757c000000f"), "note" : "blah-blah"}
Like this?
db.collection.update( { {"_id" : ObjectId("4f90cf0cd4bea011930001a3"), "third_id" : ObjectId("5edf056800126757c000000f")}, , true )
Tutorial says:
db.collection.update( criteria, objNew, upsert, multi )
Arguments:
criteria – query which selects the record to update;
objNew – updated object or $ operators (e.g., $inc) which manipulate the object
upsert – if this should be an “upsert” operation; that is, if the record(s) do not exist,
insert one. Upsert only inserts a single document.
multi – indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.
Since you only want to update a single document and you aren’t trying to do an upsert and you don’t want to replace the whole document but just one field, you can do: