Node.js 在NodeJS上获取Firestore中集合中的所有数据

Node.js 在NodeJS上获取Firestore中集合中的所有数据,node.js,firebase,collections,google-cloud-firestore,Node.js,Firebase,Collections,Google Cloud Firestore,我现在开始使用Firestore,遇到了一些我找不到答案的问题 我有以下集合/文档设置: * client (collection) - client1 (document) [...] - client2 (document) name: placholder projectId: 1234 url: string * apiKeys (collection) - platform1 (document)

我现在开始使用Firestore,遇到了一些我找不到答案的问题

我有以下集合/文档设置:

* client (collection)
  - client1 (document)
    [...]
  - client2 (document)
      name: placholder
      projectId: 1234
      url: string
      * apiKeys (collection)
        - platform1 (document)
          name: name
          key: key
        - platform2 (document)
          name: name
          key: key
我想做的是获得完整的集合及其所有文档和子集合。最后,我的目标是将其保存在如下对象中:

"client": {
    "client1": {...},
    "clinet2": {
        "name": "name",
        "projectId": 1234,
        "url": "http://localhost:5001",
        "apiKeys": {
            "platform1": {
                "name": "name",
                "apiKey": "key"
            },
            "platform2": {
                "name": "name",
                "apiKey": "key"
            }
        }
    }
}
这在Firestore中是可能的吗?如果是的话,我怎样才能做到这一点(最好是不循环,不单独获取所有内容)?任何教程、文档或现有代码都将不胜感激

谢谢,
Jan

目前,在Node.js中“不循环并单独获取所有内容”是无法做到这一点的

你必须:

  • 通过使用
    get()
    forEach()
    方法循环访问客户端集合,如下所示:
  • 对于每个客户机文档,以相同的方式在其
    apiKeys
    集合上循环
  • 您可能应该使用
    Promise.all()
    并行执行不同的异步
    get()
    操作



    与使用标准JavaScript库相比,使用Node.js可能有一个小优势,即可以使用
    getCollections()
    方法列出文档引用的子集合(该方法不适用于Web/JavaScript)。但由于您的
    apikees
    系列似乎有一个已知的名称,这并没有带来任何真正的区别。

    感谢雷诺·塔内克的回答。我写了一些代码来获取数据。它只是循环所有集合,获取其中的所有文档,循环这些集合中的所有集合,并获取第二个集合中的所有文档。它将其保存为全局变量

    代码可能更好,但现在还可以

    如果你有任何问题,请告诉我

    clients = db.collection('client')
    
    global.config.client = {}
    
    // We're gonna get all clients and their config files.   
    // Very shitty way to do this, but cloudFirestore does not allow us to fetch a whole collection as an object.
    // Looping the main client collection, getting all documents, looping over all collections in those also getting all documents.
    // Asked on StackOverflow, see: https://stackoverflow.com/questions/53741109/getting-all-data-in-collection-in-firestore-on-nodejs/53741417#53741417
    clients.get()
    //Get all documents in Client
    .then(documents => {
        documents.forEach(doc => {
            // Loop over all documents found in Client
            global.config.client[doc.id] = doc.data()
    
            clients.doc(doc.id).getCollections()
                //List all collections in the document
                .then(collection => {
                    // Loop over all found collections
                    collection.forEach(col => {
                        // Get all documents in found collection
                        console.log(doc.id, col.id)
                        clients.doc(doc.id).collection(col.id).get()
                            .then(documentsDeep =>
                                documentsDeep.forEach(docD => {
                                    global.config.client[doc.id][col.id] = {
                                        [docD.id]: docD.data()
                                    }
                                    console.log(docD.id)
                                }))
                            .catch(err => {
                                console.log(logMarkup, 'Error getting documents (global.config.client.settings)', err);
                            });
                    })
                })
                .catch(err => {
                    console.log(logMarkup, 'Error getting documents (global.config.client.settings)', err);
                });
        })
    })
    .catch(err => {
        console.log(logMarkup, 'Error getting documents (global.config.client.settings)', err);
    });   
    

    谢谢你的回复!很高兴知道我没有遗漏任何东西,如果我写了一个解决方案,结果证明它是不需要的,那将是一个遗憾。我将尝试为此编写一些灵活的代码,并在这里作为答案与大家分享!