Firestore - 获取文档集合

esk*_*kan 7 firebase google-cloud-firestore

我想自动化firestore数据库的备份过程.我们的想法是遍历根文档以构建JSON树.但我没有找到一种方法来获取文档的所有集合.我想这是可能的,因为在firestore控制台中我们可以看到树.有任何想法吗 ?

Ryo*_*awa 10

firebase.initializeApp(config);

const db = firebase.firestore();

db.settings({timestampsInSnapshots: true});

const collection = db.collection('user_dat');

collection.get().then(snapshot => {

  snapshot.forEach(doc => {

    console.log( doc.data().name );    
    console.log( doc.data().mail );

  });

});
Run Code Online (Sandbox Code Playgroud)


Yan*_*ang 8

更新
API 已更新,现在函数是 .listCollections() https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#listCollections


getCollections()方法可用于 NodeJS。

示例代码:

    db.collection("Collection").doc("Document").getCollections().then((querySnapshot) => {
    querySnapshot.forEach((collection) => {
        console.log("collection: " + collection.id);
        });
    });
Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 6

它可能在网络上(客户端js)

db.collection('FirstCollection/' + id + '/DocSubCollectionName').get().then((subCollectionSnapshot) => {
    subCollectionSnapshot.forEach((subDoc) => {
        console.log(subDoc.data());
    });
});
Run Code Online (Sandbox Code Playgroud)

感谢@marcogramy评论


Sam*_*ern 5

如果您使用的是Node.js服务器SDK,则可以使用以下getCollections()方法DocumentReferencehttps : //cloud.google.com/nodejs/docs/reference/firestore/0.8.x/DocumentReference#getCollections

此方法将返回一个CollectionReference对象数组的promise,可用于访问集合中的文档。

  • 没错,目前仅在服务器端SDK中实现。 (2认同)
  • 在网络端,我已经实现了以下解决方法:db.collection(firstCollections +'/'+ docId +'/'+ subCollection).get()用于在'subCollection'中获取文档 (2认同)