# Collection.count / Query.count
Calculates the number of records in the collection or number of result records corresponding to the query statement. Acting as the management end, the cloud functions can calculate the number of records in all the collections (The calculation on Mini Program end may be subject to permissions. See count of Mini Program).
Function signature is shown as below:
function count(): Promise<Result>
Description of return value
The results of resolve
and reject
for Promise
are defined as below:
Result Description | |
---|---|
resolve | The result of query. The Result is defined as below. |
reject | Reason for failure |
Result description
Result
from resolve
is an object with the following structure:
Field | Type | Description |
---|---|---|
total | number | Number of results |
Sample code
Get 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' // Enter the current user openid
}).count()
}