updateOne

更新时间:
复制 MD 格式

Updates a single record.

Method definition

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

Request parameters

Field

Type

Required

Description

filter

Object

Yes

The filter condition.

update

Object

Yes

This document has been updated.

options

Object

No

Control options.

The options parameter is defined as follows:

Field

Type

Required

Description

upsert

Boolean

No

Specifies whether to insert the document if no match is found. Default value: false.

Examples

  • Find the first record in the users collection where the name field is `jerry` and update its age field to 10.

    mpserverless.db.collection('users').updateOne({
        name: 'jerry'
    }, {
        $set: 
        {
            age: 10
        }
    })
    .then(res => {})
    .catch(console.error);
  • Find the first record in the users collection where the age field is greater than 18 and update its name and age fields. Other fields are retained. If no matching record is found, insert {name: "Smith", age: 22} as a new record.

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