find

更新时间:
复制 MD 格式

Finds all records in a collection that meet the specified criteria.

Method definition

find(query?: object, options?: object): Promise<MongoResult>

Request parameters

Note

A single request can return a maximum of 500 documents.

Field name

Type

Required

Description

query

Object

No

The query conditions for the database operation.

options

Object

No

Control options.

The options parameter:

Field name

Type

Required

Description

limit

Number

No

The maximum number of documents to return. The maximum and default value is 500.

skip

Number

No

The number of documents to skip.

sort

Object

No

Specify the field to sort by. Use 1 and -1 to specify the sort order. Where:

  • 1: Specifies ascending order.

  • -1: Specifies descending order.

projection

Object

No

Specifies the fields to return. Fields with a value of 1 are returned. Fields with a value of 0 are not returned.

hint

Object

No

Specifies the index to use for the query.

maxTimeMS

Number

No

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

Request examples

  • Finds records in the `users` collection where the `age` is greater than 18, sorts them by name in ascending order, and returns the `name` field for the 11th to the 20th records. The `_id` field is also returned by default.

    mpserverless.db.collection('users').find( {
        age: { $gt: 18 }
    }, {
        projection: { name: 1 },
        sort: { name: 1 },
        skip: 10,
        limit: 10,
    } )
    .then(res => {})
    .catch(console.error);
  • Sorts all records in the `users` collection by name in ascending order and returns the `name` field for the 11th to the 20th records. The `_id` field is also returned by default.

    mpserverless.db.collection('users').find( {}, {
        projection: { name: 1 },
        sort: { name: 1 },
        skip: 10,
        limit: 10,
    })
    .then(res => {})
    .catch(console.error);
    
  • Finds all users in the `users` collection whose name contains 'Wang' using the `$regex` operator.

    mpserverless.db.collection('users').find( {
       name: { $regex: 'Wang' }
    })
    .then(res => {})
    .catch(console.error);

  • Finds users in the `users` collection who are older than 18 or whose name is 'Li Si' using the logical `$or` operator.

    mpserverless.db.collection('users').find( { 
        $or: [
                   { name: "Li Si" },
                   { age: { $gt: 18 } }
        ] 
    } )
    .then(res => {})
    .catch(console.error);

Result example

Successful request:

{
    "affectedDocs": 2,
    "result": [
                    {
                        "_id": "630f2bc8f5cf3ad9f4345efc",
                        "name": "Zhang San",
                        "age": 18
                    },
                    {
                        "_id": "630f2bc8f5cf3ad9f4345efd",
                        "name": "Li Si",
                        "age": 19
                    }
                ],
    "success": true
}