Finds a single record.
Method definition
findOne(query?: object, options?: object): Promise<MongoResult>Request parameters
Field name | Type | Required | Description |
|---|---|---|---|
| Object | No | The query conditions for the database operation. |
| Object | No | Control options. |
The options parameter contains the following fields:
Field name | Type | Required | Description |
|---|---|---|---|
| Number | No | The number of documents to skip. |
| Object | No | Specifies the field for sorting and the sort order. Use
|
| Object | No | Specifies the fields to return using projection operators. Fields set to |
| Object | No | Specifies the index to use for the query. |
| Number | No | The running time in milliseconds. The default value is 1000. The maximum value is 3000. |
Examples
Find a record in the users collection where the
agefield is greater than 18. The query sorts the results by thenamefield in ascending order, skips 10 records, and returns thenamefield of the first match. The_idfield is also returned by default. The query uses thenamefield as the index instead of_id.mpserverless.db.collection('users').findOne({ age: { $gt: 18 } }, { projection: { name: 1 }, sort: { name: 1 }, skip: 10, hint: { name: 1 } }) .then(res => {}) .catch(console.error);Find a record in the users collection. The query sorts the records by the
namefield in ascending order, skips 10 records, and returns thenamefield of the first match. The_idfield is also returned by default. The query uses thenamefield as the index instead of_id.mpserverless.db.collection('users').findOne({}, { projection: { name: 1 }, sort: { name: 1 }, skip: 10, hint: { name: 1 } }) .then(res => {}) .catch(console.error);