Finds all records in a collection that meet the specified criteria.
Method definition
find(query?: object, options?: object): Promise<MongoResult>Request parameters
A single request can return a maximum of 500 documents.
Field name | Type | Required | Description |
| Object | No | The query conditions for the database operation. |
| Object | No | Control options. |
The options parameter:
Field name | Type | Required | Description |
| Number | No | The maximum number of documents to return. The maximum and default value is 500. |
| Number | No | The number of documents to skip. |
| Object | No | Specify the field to sort by. Use
|
| Object | No | Specifies the fields to return. Fields with a value of |
| Object | No | Specifies the index to use for the query. |
| 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
}