Docs Menu

collection.updateMany()icons/link.png

On this page

  • Definition
  • Usage
  • Example
  • Parameters
  • Return Value
collection.updateMany()

Update one or more documents in a collection based on a query.

To call the collection.updateMany() action from a Function, get a collection handle with database.collection() then call the handle's updateMany() method.

const query = {};
const update = { "$mul": { "quantity": 10 } };
const options = { "upsert": false }
return itemsCollection.updateMany(query, update, options)
.then(result => {
const { matchedCount, modifiedCount } = result;
console.log(`Successfully matched ${matchedCount} and modified ${modifiedCount} items.`)
return result
})
.catch(err => console.error(`Failed to update items: ${err}`))

The collection.updateMany() action has the following form:

collection("myColl").updateMany(filter, update, options)
Parameter
Description

Query

filter: <document>

Required. A standard MongoDB query document that specifies which documents to update. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Specify an empty query ({}) to update all documents in the collection.

Update Operation

update: <document>
Required. A standard MongoDB update document that specifies the update operation to perform on all documents that match the query. You can use most update operators.

Update Options

options: <document>

A document that specifies configuration options for the query. The options document has the following form:

{
"upsert": <boolean>
}

Upsert

options.upsert: <boolean>
Optional. Default: false. A boolean that, if true, indicates that MongoDB should insert a new document that matches the query when the query does not match any existing documents in the collection.

Client Session

session: <ClientSession>
Optional. A client session that specifies the transaction context in which the operation occurs. To learn more, see Transactions.

The collection.updateMany() action returns a Promise that resolves to a document that describes the update operation.

Promise<result: document>
Value
Description

Matched Count

result.matchedCount: <integer>
The number of documents in the collection that match the provided query.

Modified Count

result.modifiedCount: <integer>
The number of documents in the collection that were modified by the update operation.

Upserted ID

result.upsertedId: <ObjectID>
The _id value of the document inserted by an upsert operation. This value is only present when the upsert option is enabled and the update query does not match any documents.
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.