簡體   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