MongoDB Tutorial

  • Home
  • Getting Started
  • CRUD
  • Indexes
Home / MongoDB CRUD / MongoDB updateMany

MongoDB updateMany

Summary: in this tutorial, you’ll learn how to use the MongoDB updateMay() method to update all the documents that match a condition.

Introduction to MongoDB updateMany() method

The updateMany() method allows you to update all documents that satisfy a condition.

The following shows the syntax of the updateMany() method:

db.collection.updateMany(filter, update, options)

In this syntax:

  • The filter is a document that specifies the condition to select the document for update. If you pass an empty document ({}) into the method, it’ll update all the documents the collection.
  • The update is a document that specifies the updates to apply.
  • The options argument provides some options for updates that won’t be covered in this tutorial.

The updateMany() method returns a document that contains multiple fields. The following are the notable ones:

  • The matchedCount stores the number of matched documents.
  • The modifiedCount stores the number of modified documents.

To form the update argument, you typically use the $set operator.

$set operator

The $set operator replaces the value of a field with a specified value. It has the following syntax:

{ $set: { <field1>: <value1>, <field2>: <value2>, ...}}

If the field doesn’t exist, the $set operator will add the new field with the specified value to the document. If you specify the field with the dot notation e.g., embededDoc.field and the field does not exist, the $set operator will create the embedded document (embedded).

MongoDB updateMany() method examples

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]}, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]}, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]}, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]}, { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]} ])

1) Using the MongoDB updateMany() method to update multiple documents

The following example uses the updateMany() method to update the documents where the value of the price field is 899:

db.products.updateMany( { price: 899}, { $set: { price: 895 }} )

In this query:

The { price : 899 } is the filter argument that specified the documents to update.

The { $set: { price: 895} } specifies the update to apply, which uses the $set operator to set the value of the price field to 895.

The query returns the following result:

{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

In this result document, the nMatched stores the number of matching documents (2) and the nModified stores the number of the updated documents (2).

To check the update, you can use the find() method to select the documents where the value of the price field is 895 as follows:

db.products.find({ price: 895 }, { name: 1, price: 1 })

The query returned the following documents:

{ "_id" : 2, "name" : "xTablet", "price" : 895 } { "_id" : 3, "name" : "SmartTablet", "price" : 895 }

The price field values have been updated successfully.

2) Using the updateMany() method to update embedded documents

The following query uses the find() method to select the documents where the value in the price field is greater than 700:

db.products.find({ price: { $gt: 700} }, { name: 1, price: 1, spec: 1 }).pretty()

The query returned the following documents:

{ "_id" : 1, "name" : "xPhone", "price" : 799, "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 } } { "_id" : 2, "name" : "xTablet", "price" : 895, "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 } } { "_id" : 3, "name" : "SmartTablet", "price" : 895, "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 } }

The following example uses the updateMany() method to update the values of the ram, screen, and cpu fields in the spec embedded documents of these documents:

db.products.updateMany({ price: { $gt: 700} }, { $set: { "spec.ram": 32, "spec.screen": 9.8, "spec.cpu": 5.66 } })

The query returned the following document indicating that the three documents have been updated successfully:

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

3) Using the MongoDB updateMany() method to update array elements

The following example uses the updateMany() method to update the first and second elements of the storage array of the documents where the _id is 1, 2 and 3:

db.products.updateMany({ _id: { $in: [1, 2, 3] } }, { $set: { "storage.0": 16, "storage.1": 32 } })

Output:

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

If you query the documents whose _id is 1, 2, and 3 from the products collection, you’ll see that the first and second elements of the storage array have been updated:

db.products.find( { _id: { $in: [1,2,3]}}, { name: 1, storage: 1} )

Output:

{ "_id" : 1, "name" : "xPhone", "storage" : [ 16, 32, 256 ] } { "_id" : 2, "name" : "xTablet", "storage" : [ 16, 32, 512 ] } { "_id" : 3, "name" : "SmartTablet", "storage" : [ 16, 32, 128 ] }

Summary

  • Use the updateMany() method to update all the documents within a collection that satisfy a condition.
  • Use the $set operator to replace the value of a field with a specified value.
  • Was this tutorial helpful ?
  • YesNo
Previous MongoDB updateOne
Next How to Use MongoDB $inc Operator in update() Method

Getting Started

  • What is MongoDB
  • Install MongoDB
  • MongoDB Basics
  • MongoDB Shell
  • MongoDB Data Types

Inserting Documents

  • insertOne
  • insertMany
  • mongoimport

Selecting Documents

  • findOne
  • find
  • Projection: Selecting Returned Fields

Comparison Query Operators

  • $eq: Equal To Operator
  • $lt: Less Than Operator
  • $lte: Less Than or Equal To Operator
  • $gt: Greater Than Operator
  • $gte: Greater Than or Equal To Operator
  • $ne: Not Equal To Operator
  • $in: In Operator
  • $nin: Not In Operator

Logical Query Operators

  • $and: Logical AND Opeartor
  • $or: Logical OR Operator
  • $not: Logical NOT Operator
  • $nor: Logical NOR Operator

Element Query Operators

  • $exists
  • $type

Array Query Operators

  • $size
  • $all
  • $elemMatch

Sorting & Limiting

  • sort(): Sorting documents
  • limit(): Limiting documents

Updating Documents

  • updateOne: Update one Document
  • updateMany: Update Multiple Documents
  • $inc: Increase / Decrease Field Value
  • $min: Update Field Value
  • $max: Update Field Value
  • $mul: Mutiply Field By a Number
  • $unset: Remove Fields
  • $rename: Rename Fields
  • Upsert

Deleting Documents

  • deleteOne
  • deleteMany

About MongoDBTutorial.com

This MongoDB Tutorial helps you master MongoDB quickly.

Recent Tutorials

  • MongoDB Indexes
  • MongoDB limit
  • MongoDB sort
  • MongoDB Upsert
  • MongoDB Projection

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Copyright © 2021 ยท By mongodbtutorial.org. All Rights Reserved.