replaceOne

Updated at:

Query for and replace an entire record.

Method definition

replaceOne(filter: object, doc: object, options?: object): Promise<MongoResult>

Request parameters

Field name

Type

Required

Description

filter

Object

Yes

The filter condition for the operation.

doc

Object

Yes

The replacement document.

options

Object

No

Control.

The options parameter:

Field name

Type

Required

Description

upsert

Boolean

No

Specifies whether to insert a new document if no match is found. The default value is false.

Examples

  • Finds the first document in the users collection where the name field is tom and the age field is 18, and replaces it with { name: 'jerry' }. If no matching document is found, no operation is performed.

    mpserverless.db.collection('users').replaceOne({
        name: 'tom',
        age: 18
    }, {
        name: 'jerry'
    })
    .then(res => {})
    .catch(console.error);
  • Finds the first document in the users collection where the name field is tom and the age field is 18, and replaces it with { name: 'jerry' }. If no matching document is found, a new document is inserted.

    mpserverless.db.collection('users').replaceOne({
        name: 'tom',
        age: 18
    }, {
        name: 'jerry'
    }, {
        upsert: true
    })
    .then(res => {})
    .catch(console.error);