O'Reilly logo

MongoDB 4 Quick Start Guide by Doug Bierer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating one or more documents

Command summary (discussed as follows):

  • db.<collection>.updateOne({<filter>},{<update>}[,{upsert:true}]);
  • db.<collection>.updateMany({<filter>},{<update>}[,{upsert:true}]);
  • db.<collection>.replaceOne({<filter>},{ // document }[,{upsert:true}]);

Both update*() and delete*() use a filter. The good news is that the filter uses exactly the same syntax as the find() command, mentioned previously. The difference between updateOne() and replaceOne() is that with the former you only need to supply the fields that need to be set or unset, whereas with the latter (for example, replaceOne()) you need to supply an entire replacement document. If the upsert parameter is set to true, a new document is inserted if the update ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required