findOneAndDelete

更新时间:
复制 MD 格式

Deletes a single record and returns the deleted record.

Method definition

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

Request parameters

Field name

Type

Required

Description

filter

Object

Yes

The query criteria for the operation.

options

Object

No

Control.

options parameter is defined as follows:

Field name

Type

Required

Description

sort

Object

No

Specifies the fields to sort by. Use 1 or -1 to specify the sort order.

  • 1: ascending order.

  • -1: descending order.

projection

Object

No

Specifies the fields to return. A value of 1 includes the field. A value of 0 excludes it. The _id field is included by default. To exclude it, you must set _id to 0.

maxTimeMS

Number

No

The running time in milliseconds. Default: 1000. Maximum: 3000.

Examples

  • Find and delete a record from the users collection where the age field is 18, and then return the deleted record. If multiple records match, the first matching record is deleted and returned.

    mpserverless.db.collection('users').findOneAndDelete({
        age: 18,
    })
    .then(res => {})
    .catch(console.error)
  • Find a record in the users collection where the age field is 18. If multiple records match, the one that is first in ascending order by name is selected. The selected record is then deleted, and its name field is returned.

    mpserverless.db.collection('users').findOneAndDelete({
        age: 18,
    }, {
        projection: { _id: 0, name: 1 },
        sort: { name: 1 },
    })
    .then(res => {})
    .catch(console.error);