Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program cloud development API statistics collection records


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Collection.count / Query.count

Count the number of collection records or the number of result records corresponding to the statistical query statement, note that this is related to the collection permission setting, a user can only count the number of records whose read permissions.

The function signature is as follows:

function count(options?: object): Promise<Result>

Parameter Description

Options is an optional parameter, a object, such as one of the three Success, Fail, Complete, indicating that the callback style is used, does not return Promise.

Field name type Required Defaults illustrate
success Function no Successful callback, callback incoming parameter Result contains the result of the query, and the result is asked below
fail Function no Failed callback
complete Function no Call the ended callback function (successful call, failure)

Return value description

If you don't pass the Options parameter, or the incoming Options parameter is not Success, Fail, the Complete field, returns a promise, otherwise no value is returned.The result of the promise's resolve and reject is as follows:

Result description
resolve The result of the query, the result is under the definition
reject Failure

Result description

The result of the Success callback and the result of Promise Resolve Result is an object of the following structure:

Field type illustrate
total number Quantity

Sample code

Get my total number of things to do

Callback style

const db = wx.cloud.database()
db.collection('todos').where({
  _openid: 'xxx' // 填入当前用户 openid
}).count({
  success: function(res) {
    console.log(res.total)
  }
})

Promise style

const db = wx.cloud.database()
db.collection('todos').where({
  _openid: 'xxx' // 填入当前用户 openid
}).count().then(res => {
  console.log(res.total)
})