Retrieves the number of records in a collection that match the filter condition. This operation scans all documents in the collection. A query timeout may occur if the collection contains many documents.
Method definition
count(query: object, options?: object): Promise<MongoResult>Request parameters
Field name | Type | Required | Description |
| Object | Yes | The filter condition. |
| Object | No | Controls. |
The options parameter contains the following fields:
Field name | Type | Required | Description |
| Number | No | The maximum number of documents to count. |
| Number | No | The number of documents to skip before the count operation begins. |
| Number | No | The running time in milliseconds. Default: 1000. Maximum: 3000. |
Request examples
Count the number of records in the
userscollection.
mpserverless.db.collection('users').count({
age: { $gt: 18 }
}).then((res) => {
console.log(res);
}).catch(console.error);Count the records in the
userscollection whereageis 18 or greater.
mpserverless.db.collection('users').count({
age: { $gt: 18 }
}).then((res) => {
console.log(res);
}).catch(console.error);Count the number of records in the myNoteBook collection where the value of the
titlefield is1and the value of theuserIdfield is2088112127936xxx. Then, print the result.
mpserverless.db.collection('myNoteBook').count({
title: "1",
userId: "2088112127936xxx",
}).then((res) => {
console.log(res);
}).catch(console.error);Result examples
Successful request
{
"affectedDocs": 2,
"result": 2,
"success": true
}Failed request
{
"success": false,
"error": {
"code": "InternalServerError",
"message": "Error in $cursor stage :: caused by :: operation exceeded time limit"
}
}