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)
更新
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)
它可能在网络上(客户端js)
db.collection('FirstCollection/' + id + '/DocSubCollectionName').get().then((subCollectionSnapshot) => {
subCollectionSnapshot.forEach((subDoc) => {
console.log(subDoc.data());
});
});
Run Code Online (Sandbox Code Playgroud)
感谢@marcogramy评论
如果您使用的是Node.js服务器SDK,则可以使用以下getCollections()方法DocumentReference:https :
//cloud.google.com/nodejs/docs/reference/firestore/0.8.x/DocumentReference#getCollections
此方法将返回一个CollectionReference对象数组的promise,可用于访问集合中的文档。
| 归档时间: |
|
| 查看次数: |
29309 次 |
| 最近记录: |