繁体   English   中英

您如何使用 admin SDK 计算 firestore 集合中的文档?

[英]How do you count documents in a firestore collection using the admin SDK?

我试过CollectionReference count 这里的文档( admin.firestore().collection("users").count() )但它说

类型错误:collectionReference.count 不是一个函数

我也试过:( admin.firestore().collection("users").countDocuments()

还是:

类型错误:collectionReference.countDocuments 不是函数

要获取集合的大小,请执行以下操作:

根据 Count 文档:服务器计算计数,并仅将结果(一个整数)传输回您的应用程序,与执行完整查询相比,节省了计费文档读取和传输的字节数。

// Below is in preview 
const collectionRef = admin.firestore().collection('users');
const snapshot = await collectionRef.count().get();
console.log(snapshot.data().count);

// below will fetch all the data and count it. It is more expensive.
const snapshot = await admin.firestore().collection("users").get();
// your function should be async
console.log('collection size is: ', snapshot.size);

// or
console.log('collection size is: ', snapshot.docs.length);

您必须在获取长度之前获取集合。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM