Firebase查询收集和合并子收集数据 [英] Firebase Query Collection And Merge Subcollection Data

查看:98
本文介绍了Firebase查询收集和合并子收集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来获取文档及其子收集数据.

I'm trying to figure out a way to get a collection of documents along with their subcollection data.

例如,我的树是...

For example, my tree is...

-locations
 -{locationId}
   -historic
     -april
       -airTemp: 12.4
       -displayOrder: 4
     -august
       -airTemp: 14.5
       -displayOrder: 9
     -december
       -airTemp: 13.5
       -displayOrder: 12
     etc..

...其中 locationId 是一个文档,而 historic 是其中包含每月文档的子集合.

...where locationId is a document and historic is the subcollection with monthly documents in it.

我知道如何获取顶级集合项并将其存储到数组中,但是我也想将其子集合数据(即jan,feb等)添加到每个文档中.

I know how to get the top level collection items and store it into an array but I want to add their subcollection data (i.e. the jan, feb, etc.) into each document as well.

  var locations = []

  let ref = db.collection('locations')
  db.collection('locations').get()
  .then(snapshot => {
      snapshot.forEach(doc => {
          let location = doc.data()
          location.id = doc.id
          location.name = doc.name

          // get the historic subcollection data here

      })
  })

如何从每个对象中获取合并的数据(集合和子集合),然后将其推入数组中?

How can I get the combined data (collection and subcollection) from each object and then push it into an array?

赏金结构错误 每个月应该是自己的对象

bounty is wrong structure each month should be its own object

推荐答案

对于移动/网络客户端库,无法通过一个查询获取Firestore文档的数据及其子集合文档的数据.

There is no way, with the mobile/web client libraries, to get, in one query, the data of a Firestore document and the data of its sub-collection(s) documents.

甚至无法获取文档的子集合列表(使用移动/网络客户端库),请参见

There is even no way to get the list of the sub-collections of a document (with the mobile/web client libraries), see https://firebase.google.com/docs/firestore/query-data/get-data#list_subcollections_of_a_document

因此,您需要知道子集合的名称才能查询它们(这是您的情况(名称='historic').

So you need to know the name of the sub-collections in order to query them, which is your case (name = 'historic').

您可以执行以下操作.首先,获得所有父文档,同时使用 Promise.all()并行查询所有历史"子集合.

You could do as follows. First you get all the parent documents and at the same time you use Promise.all() to query, in parallel, all the 'historic' sub-collections.

    var db = firebase.firestore();

    var promises = []
    db.collection('locations').get()
        .then(snapshot => {
            snapshot.forEach(doc => {
                let location = doc.data()
                location.id = doc.id
                location.name = doc.data().name
                console.log(location);
                promises.push(doc.ref.collection('historic').get());
            })
            return Promise.all(promises);
        })
        .then(results => {
            results.forEach(querySnapshot => {
                querySnapshot.forEach(function (doc) {
                    console.log(doc.id, " => ", doc.data());
                });
            });
        });

请注意,结果 results 数组的顺序与 promises 数组的顺序完全相同.

Note that the order of the results array is exactly the same than the order of the promises array.

还请注意,要获取文档项目的值(例如 name ),您需要执行 doc.data().name ,而不是 doc.名称.

Also note that to get the value of a document item (e.g. name) you need to do doc.data().name and not doc.name.

这篇关于Firebase查询收集和合并子收集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆