updateMany

Updated at:

Updates multiple records in a collection.

Method signature

updateMany(filter: object, update: object, options?: object): Promise<MongoResult>

Request parameters

Field name

Type

Required

Description

filter

Object

Yes

The filter condition.

update

Object

Yes

The update document.

options

Object

No

Control

The options parameter is described as follows:

Field name

Type

Required

Description

upsert

Boolean

No

Specifies whether to insert the document if no match is found. The default is false.

Examples

  • Finds all records in the users collection where the name field is `jerry` and updates their age field to 10.

    mpserverless.db.collection('users').updateMany({
        name: 'jerry'
    }, {
        $set: 
        {
            age: 10
        }
    })
    .then(res => {})
    .catch(console.error);
  • Finds all records in the users collection where the age field is greater than 18 and updates their `name` and `age` fields. Other fields remain unchanged. If no matching records are found, a new record {name: "Smith", age: 22} is inserted.

    mpserverless.db.collection('users').updateMany({
        age: { $gt: 18 }
    }, {
        $set: 
        {
            name: "Smith",
            age: 22
        }
    }, {
        upsert: true
    })
    .then(res => {})
    .catch(console.error);