简体   繁体   English

尝试在 Node.js 中加载所有 firestore 文档

[英]Trying to load all firestore documents in Node.js

I can request a specific document in a collection successfully.我可以成功请求集合中的特定文档。 But I need to be able to query all documents, as Firestore (unlike the realtime database) doesn't support exporting.但我需要能够查询所有文档,因为 Firestore(与实时数据库不同)不支持导出。

I know the following is not possible, but surely there is some way to export a whole collection?我知道以下是不可能的,但肯定有某种方法可以导出整个集合吗?

router.get('/firebase', function (req, res) {
    var admin = require('firebase-admin');
    const firebaseConfig = require('../firebaseConfig.json');

    admin.initializeApp({
        credential: admin.credential.cert(firebaseConfig),
        databaseURL: 'https://***.firebaseio.com'
    });

    let db = admin.firestore();
    let cityRef = db.collection('marketing').doc('*');
    let getDoc = cityRef.get()
        .then(doc => {
            if (!doc.exists) {
                console.log('No such document!');
            } else {
                console.log('Document data:', doc.data());
            }
        })
        .catch(err => {
            console.log('Error getting document', err);
        });
});

A refactored version of this answer by Doug Stevenson Doug Stevenson对此答案的重构版本

router.get('/firebase', async function (req, res) {
    var admin = require('firebase-admin');
    const firebaseConfig = require('../firebaseConfig.json');

    admin.initializeApp({
        credential: admin.credential.cert(firebaseConfig),
        databaseURL: 'https://gundies.firebaseio.com'
    });

    let db = admin.firestore();

    const snapshot = await db.collection("marketing").get();

    const docs = snapshot.docs.map(doc => doc.data())

The variable 'docs' will hold all your data!变量“文档”将保存您的所有数据!

db.collection('marketing').doc('*') is not the correct syntax. db.collection('marketing').doc('*')语法不正确。 There are no document wildcards in Firestore queries. Firestore 查询中没有文档通配符。 Just use db.collection('marketing').get() to get a QuerySnapshot with all the documents in the collection.只需使用db.collection('marketing').get()即可获取包含集合中所有文档的 QuerySnapshot。

It works just like you see in the documentation for queries , except you are not specifying any filter to limit the result set.它的工作方式与您在 查询文档中看到的一样,只是您没有指定任何过滤器来限制结果集。

As per Doug's answer, not loading a .doc() allowed me to get all the records.根据 Doug 的回答,不加载.doc()允许我获取所有记录。 I had tried this earlier but the console.log was quite difficult to read.我之前试过这个,但是 console.log 很难阅读。 Adding a forEach from this StackOverflow answer helped out.从这个StackOverflow 答案中添加一个 forEach 很有帮助。

router.get('/firebase', function (req, res) {
    var admin = require('firebase-admin');
    const firebaseConfig = require('../firebaseConfig.json');

    admin.initializeApp({
        credential: admin.credential.cert(firebaseConfig),
        databaseURL: 'https://***.firebaseio.com'
    });

    let db = admin.firestore();

    db.collection('marketing').get()
        .then(snapshot => {
            snapshot.forEach(doc => {
                console.log(doc.id, '=>', doc.data());
            });
        })
        .catch(err => {
            console.log('Error getting document', err);
        });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM