【问题标题】:Firestore read counts of documents sizeFirestore 读取文档大小的计数
【发布时间】:2021-12-03 19:40:43
【问题描述】:
我正在尝试获取特定集合中的文档数量,如下所示:
const userCol = db.collection('users').get().then(queryResult => {
if (queryResult.exists)
console.log(queryResult.size); // amount of documents
});
所以当我使用.get() 读取文件时,这意味着我得到x reads 的文件数量存在吗?
如果这是真的,那么有没有一种方法可以只获取文件的数量而不必阅读每一个文件?
【问题讨论】:
标签:
javascript
google-cloud-firestore
【解决方案1】:
首先,在您编写的函数中,read 的量只会增加 1 倍。也就是说,它不依赖于文档的数量。这取决于使用get() 函数的次数。每次使用 get() 函数意味着 1 次读取 计数。您将访问 users 集合中的所有文档,但 read 的数量将增加 1,因为您只使用了一次 get()。
注意:.exist 的定义适用于文档,而不是集合。
使用存在命令,您可以轮询文档的存在,而不是集合。
const userCol = db.collection('users').get().then(queryResult => {
console.log(queryResult.size); // amount of documents
});
或者您也可以使用.listDocuments() 属性来获取集合中的文档列表。
const userCol = db.collection('users').listDocuments().then(documentsList => {
console.log(documentsList.length); // amount of documents
});
两种方式都会增加 1 的读取次数。