简体   繁体   English

如何在文档中计算 Firebase 和 collections

[英]How can I count in Firebase the collections in a document

I know how to count how many Documents are in a collection in Firebase but how does it work the other way around.我知道如何计算 Firebase 中的集合中有多少文档,但反过来它是如何工作的。 I have several collections in a document and I need to find out how many.我在一个文档中有几个 collections,我需要找出有多少。 (In my example it would be 2) (在我的示例中为 2)

My Setup:我的设置:

Collection     Document      Collections  Documents
[USERID]   -   [TESTS]   -   [1]   -      [ANSWERS]
                                          [RESULTS]
                         
                             [2]   -      [ANSWERS]
                                          [RESULTS]

I tried the following but it does not work:我尝试了以下方法,但它不起作用:

let refTest_1 = db.collection(USERID).doc('TESTS')
let testData_1
refTest_1.get().then(doc => {
  //testData_1 = doc.data()
  testData_1 = doc.size
}).then(function() {
  //console.log(Object.keys(testData_1).length)
  console.log(testData_1)
}).catch(error => {
  console.log("Error: " + error.message)
})  

Does anybody know how this works?有人知道这是如何工作的吗?

Thanks!谢谢!

The official Firebase Web Client has no implemented method to get all collections of an object.官方的 Firebase Web 客户端没有实现方法来获取 ZA8CFDE6331BD59EB4B66AC96F8911C 的所有 collections。 You can take a look at the documentation here .您可以在此处查看文档。 In Node.js you could just use the .listCollections() method as described in the same documentation.在 Node.js 中,您可以使用相同文档中描述的.listCollections()方法。 Afterwards, you would have to get all the documents in the collections and count them manually in js.之后,您必须将 collections 中的所有文档获取并在 js 中手动计数。

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

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