This post discusses common update syntax for mongodb with a Demo.

Syntax


db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>
   }
)
    
  • Query : Query Selectors
  • Update : Modifications to apply
  • Upsert : true means insert if document does not match. False means do not insert if document does not match. Default value is false.
  • Multi : true means update all matching document, false means update single document. Default value is false.
  • writeConcern : Read Official Documentation.
  • collation : Specify collation to use for the update operation.

Initialize Person Collection

Create person collection and insert some records


> db.createCollection("person");
> db.person.insert([
	{ "firstName" : "Hiral", "lastName" : "Patel", "Profession" : "Developer" },
	{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician" },
	{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" },
	{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" },
	{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
  ]);
    

Update/Replace a document

Update/Replace a document with a lastName = Patel. Following query will replace the first matching document with modified fields.


> db.person.update({"lastName": "Patel"}, {"Website" : "www.smoothprogramming.com"});
> db.person.find({}, {_id:0})
{ "Website" : "www.smoothprogramming.com" }
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
    

Update specific fields of a Document

Use $set to update a specific fields of a document. Following query will update provided fields for matching document.


> db.person.update({"firstName" : "Rajesh"}, {$set : {"Website" : "www.rajeshmusic.com"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.person.find({}, {_id:0})
{ "Website" : "www.smoothprogramming.com" }
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician", "Website" : "www.rajeshmusic.com" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
    

Update multiple documents

Set multi = true to update multiple matching documents.


> db.person.update({"lastName" : "Patel"}, {$set : {"Gender" : "Male"}}, {multi : true});
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.person.find({}, {_id:0})
{ "Website" : "www.smoothprogramming.com" }
{ "firstName" : "Rajesh", "lastName" : "Patel", "Profession" : "Musician", "Website" : "www.rajeshmusic.com", "Gender" : "Male" }
{ "firstName" : "Sandeep", "lastName" : "Patel", "Profession" : "Player", "Gender" : "Male" }
{ "firstName" : "Harry", "lastName" : "Patel", "Profession" : "Actor", "Gender" : "Male" }
{ "firstName" : "Sandeep", "lastName" : "Trivedi", "Profession" : "Electician" }
    

Conclusion

MongoDB update can achieve desire update behaviour if correct settings are provided.

References

update documentation
$set documentation