简体   繁体   English

查询Firestore集合中的所有文档而不进行循环

[英]Query all Documents in a Firestore Collection without Looping

I have been working on transitioning a small Firebase project to the new, similar Firestore db because of its more queryable nature, but I am having a trouble with a simple query problem. 我一直致力于将一个小型Firebase项目转换到新的类似Firestore数据库,因为它具有更多可查询性,但我遇到了一个简单的查询问题。

I am trying to get all documents in a collection, in the case each document is a club and the collection is all of the clubs. 我试图将所有文件都收集在一个集合中,如果每个文档都是一个俱乐部,那么该集合就是所有的俱乐部。

In Firebase, I was able to do something like: 在Firebase中,我能够执行以下操作:

export function fetchClubs() {
    const Club = firestore.ref('clubs');
    return dispatch => {
        Club.on('value', snapshot => {
            dispatch({
                type: FETCH_CLUBS,
                payload: snapshot.val()
            })
        })
    }
}

snapshot would have all the information i needed inside of it. snapshot将拥有我需要的所有信息。

I am trying to replicate this with Firestore, but have an only been able to come across a solution that involves making a new array and looping over the results then adding each result to that. 我试图用Firestore复制它,但是只能遇到一个解决方案,它涉及创建一个新数组并循环结果然后将每个结果添加到该数据中。

Here is my Firestore attempt using the slightly different Firestore syntax 这是我的Firestore尝试使用稍微不同的Firestore语法

export function fetchClubsStore() {
    const Clubs = firestore.collection('clubs');
     return dispatch => {
        Clubs.get().then(querySnapshot => {
            console.log("clubs snapshot: ", querySnapshot)
            dispatch({
                type: FETCH_CLUBS,
                payload: querySnapshot.val()
            })
        })
    }
}

If you have any ideas, all help will be greatly appreciated! 如果您有任何想法,将非常感谢所有帮助! Thanks! 谢谢!

(this is my first stackoverflow post, go easy! :) ) (这是我的第一个stackoverflow帖子,转到简单!:))

Edit: 编辑:

Example from Firestore docs of how to get all documents in a collection. Firestore文档中有关如何获取集合中所有文档的示例。

db.collection("cities").where("capital", "==", true)
.get()
.then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
})
.catch(function(error) {
    console.log("Error getting documents: ", error);
});

Edit 2: 编辑2:

My new solution to get the documents in a usable array by using .docs and .data() . 我的新解决方案是使用.docs和.data()将文档放入可用的数组中。 Still seems inefficient, as I am having to loop over the doucments and extract the usable information one by one. 仍然看起来效率低下,因为我不得不循环遍历doucments并逐个提取可用信息。

export function fetchClubsStore() {
const Clubs = firestore.collection('clubs');
var clubs = [];
return dispatch => {
    Clubs.get().then(querySnapshot => {
        console.log("clubs snapshot: ", querySnapshot.docs)
        _.each(querySnapshot.docs, function(data){
            clubs.push(data.data());
        })
        dispatch({
            type: FETCH_CLUBS,
            payload: clubs
        })
    })
}

} }

The difference between Firebase Realtime Database and Firestore is that RTDB is basically just a cloud hosted JSON object. Firebase实时数据库和Firestore之间的区别在于,RTDB基本上只是一个云托管的JSON对象。 So there is no concept of documents and there is no collections of data. 因此,没有文档的概念,也没有数据集合。

The value object in a query snapshot that you retrieve from RTDB is just a JSON object. 从RTDB检索的查询快照中的值对象只是一个JSON对象。 Hence you can access first level nodes by their keys (and also any level nodes by their keys) 因此,您可以通过其键(以及按键的任何级别节点)访问第一级节点

If you're coming from RTDB you'll notice the difference that Cloud Firestore has collections and documents. 如果您来自RTDB,您会注意到Cloud Firestore具有集合和文档的区别。 So your query results will inherently be collections of data rather than a JSON object. 因此,您的查询结果本质上是数据集合而不是JSON对象。 The documents that are retrieved from a query can not be grabbed as a single big JSON object. 从查询中检索的文档不能作为单个大JSON对象获取。

Looping over the query result is the correct way to work with Cloud Firestore. 循环查询结果是使用Cloud Firestore的正确方法。

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

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