Firestore - Get document collections

后端 未结 5 2053
谎友^
谎友^ 2020-12-28 21:32

i would to automate backup process of a firestore database. The idea is to loop over root document to build a JSON tree. but i didn\'t find a way to get all collections ava

5条回答
  •  -上瘾入骨i
    2020-12-28 22:14

    As mentioned by others, on the server side you can use getCollections(). To get all the root-level collections, use it on the db like so:

    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}`);
        });
    });
    

提交回复
热议问题