Firestore - 获取文档集合
Firestore - Get document collections
我会自动执行 firestore 数据库的备份过程。
我的想法是遍历根文档来构建一个 JSON 树,但我没有找到一种方法来获取文档的所有集合。我想这可能是因为在 firestore 控制台中我们可以看到树。
有什么想法吗?
如果您使用的是 Node.js 服务器 SDK,则可以在 DocumentReference
上使用 getCollections()
方法:
https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/DocumentReference#getCollections
此方法将 return 承诺一个 CollectionReference
对象数组,您可以使用这些对象访问集合中的文档。
更新
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);
});
});
它可以在网络上(客户端 js)
db.collection('FirstCollection/' + id + '/DocSubCollectionName').get().then((subCollectionSnapshot) => {
subCollectionSnapshot.forEach((subDoc) => {
console.log(subDoc.data());
});
});
感谢@marcogramy 评论
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 );
});
});
正如其他人所提到的,在服务器端,您可以使用 getCollections().
要获取所有根级集合,请像这样在数据库上使用它:
const serviceAccount = require('service-accout.json');
const databaseURL = 'https://your-firebase-url-here';
const admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseURL
});
const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
db.getCollections().then((snap) => {
snap.forEach((collection) => {
console.log(`paths for colletions: ${collection._referencePath.segments}`);
});
});
我会自动执行 firestore 数据库的备份过程。
我的想法是遍历根文档来构建一个 JSON 树,但我没有找到一种方法来获取文档的所有集合。我想这可能是因为在 firestore 控制台中我们可以看到树。
有什么想法吗?
如果您使用的是 Node.js 服务器 SDK,则可以在 DocumentReference
上使用 getCollections()
方法:
https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/DocumentReference#getCollections
此方法将 return 承诺一个 CollectionReference
对象数组,您可以使用这些对象访问集合中的文档。
更新
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);
});
});
它可以在网络上(客户端 js)
db.collection('FirstCollection/' + id + '/DocSubCollectionName').get().then((subCollectionSnapshot) => {
subCollectionSnapshot.forEach((subDoc) => {
console.log(subDoc.data());
});
});
感谢@marcogramy 评论
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 );
});
});
正如其他人所提到的,在服务器端,您可以使用 getCollections().
要获取所有根级集合,请像这样在数据库上使用它:
const serviceAccount = require('service-accout.json');
const databaseURL = 'https://your-firebase-url-here';
const admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: databaseURL
});
const db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
db.getCollections().then((snap) => {
snap.forEach((collection) => {
console.log(`paths for colletions: ${collection._referencePath.segments}`);
});
});