May 20, 2021 WeChat Mini Program Development Document
Counting the number of collection records or the number of result records corresponding to statistical query statements, the cloud function side because it belongs to the management side, so you can count the number of records for all collections (small terminal statistics will be limited by permissions, can be seen on the small terminal count)
The function signature is as follows:
function count(): Promise<Result>
Returns a description of the value
The results of Promise's resolve and reject are defined as follows:
Description of the results | |
---|---|
resolve | The results of the query, the Result definition can be found below |
reject | The reason for the failure |
Result description
The result of resolve is an object with the following structure:
Field | Type | Description |
---|---|---|
total | number | The number of results |
The sample code
Get the total number of my to-dos
Promise style
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
return await db.collection('todos').where({
_openid: 'xxx' // 填入当前用户 openid
}).count()
}