文档

count

更新时间:

获取集合中符合条件的记录数量。该操作会对集合中的文档进行扫描,当文档数量较大时易出现查询超时的情况。

方法定义

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

请求参数

字段名

类型

必填

说明

query

Object

过滤条件。

options

Object

控制项。

options 参数定义:

字段名

类型

必填

说明

limit

Number

限制count的文档数量。

skip

Number

count前跳过的文档数量。

maxTimeMS

Number

执行时间(毫秒),默认值:1000,最大值:3000。

请求示例

  • 统计所有存储在集合users中的记录的数量。

mpserverless.db.collection('users').count({
    age: { $gt: 18 }
}).then((res) => {
    console.log(res);
}).catch(console.error);
  • 查找集合users 中所有age 大于等于18的记录数量。

mpserverless.db.collection('users').count({
    age: { $gt: 18 }
}).then((res) => {
    console.log(res);
}).catch(console.error);
  • 查找集合 myNoteBook 中 title 字段为 1 ,userId 字段为 2088112127936xxx 的记录数量,并打印结果。

mpserverless.db.collection('myNoteBook').count({ 
    title: "1", 
    userId: "2088112127936xxx", 
}).then((res) => {
    console.log(res);
}).catch(console.error);

结果示例

  • 请求成功

{
    "affectedDocs": 2,
    "result": 2,
    "success": true
}
  • 请求失败

{
    "success": false,
    "error": {
        "code": "InternalServerError",
        "message": "Error in $cursor stage :: caused by :: operation exceeded time limit"
    }
}

  • 本页导读 (0)