简体   繁体   English

在 firestore(网络)中加入集合和子集合

[英]Joining collection and sub-collection in firestore (web)

I'm playing around trying to learn Firestore and I'm experiencing some issues regarding joining data from a collection and a sub-collection.我正在尝试学习 Firestore,但在连接集合和子集合中的数据时遇到了一些问题。 The name of the sub-collection is known players .子集的名称是已知players

-match_stats (collection)
  |_ document with random ID
    |_ map (field - string)
    |_ scorehome (field - Number)
    |_ scoreaway (field - Number)
      |_ Sub-collection (named: Players)
        |_ documents with random ids
          |_ Field (Player names, ex. Christopher)
            |_ playerkills
            |_ playerdeaths

I've been able to console.log each collection separately, but I want the collection and the sub collection to be included in the same object, so I can combine the data.我已经能够分别对每个集合进行console.log ,但我希望集合和子集合包含在同一个 object 中,因此我可以合并数据。 I'm also open to other suggestions on how to solve this problem.我也愿意接受有关如何解决此问题的其他建议。

console.log 的结果

 firebase.initializeApp(firebaseConfig); const db = firebase.firestore(); export default { data() { return { matchInfo: [], }; }, methods: { readMatches() { this.matchInfo = []; db.collection("match_stats").get().then(matchQuerysnapshot => { matchQuerysnapshot.docs.forEach(doc => { console.table(doc.data()); db.collection("match_stats").doc(doc.id).collection("players").get().then(playersQuerySnapshot => { playersQuerySnapshot.docs.forEach(doc => { console.table(doc.data()); }) }) }) }) }, }, mounted() { this.readMatches(); }, };

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 players sub-collections.首先,您获得所有父文档,同时使用 Promise.all() 并行查询所有players子集合。

    var db = firebase.firestore();

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

Note that the order of the results array is exactly the same than the order of the promises array.请注意, results数组的顺序与promises数组的顺序完全相同。

I based my answer on this similar question making changes to adapt to your question.我的回答基于这个类似的问题,并进行了更改以适应您的问题。

Fire store has its own hook for grabbing data while page loads. Fire store 有自己的钩子,用于在页面加载时抓取数据。

data() {
    return {

      matchInfo: [],

    };
  },
firestore() {
      return {
        matchInfo: db.collection('players')
        ... The rest of your code ...        

      }
    },

暂无
暂无

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

相关问题 未创建文档的 Cloud Firestore 子集合 - Cloud Firestore Sub-Collection on a Document not Creating 将文档动态添加到 Firestore 子集合 - SwiftUI - Add document to Firestore sub-collection dynamically - SwiftUI 从 Firestore 的子集合中获取实时数据 - Getting realtime data from sub-collection in Firestore 如何按firebase中的子集合中的字段排序flutter中的云Firestore - How to sort by a field in a sub-collection in firebase cloud firestore in flutter 如何根据 Cloud Firestore 中的主集合字段和子集合字段查询子集合中的文档? - How to query documents from a sub-collection, based on a main collection's field and a sub-collection's fields in cloud firestore? 使用云功能 typescript 在 Firestore 中创建子子集合时如何更新子集合文档 - How to update a sub-collection document when sub sub-collection is created in firestore using cloud functions typescript 如何在不使用 get() 的情况下根据父集合中的字段实现对 firestore 子集合的控制? - How to achieve control over a firestore sub-collection depending on a field in a parent collection without using get()? 如何通过 Firestore 更新 Flutter 评论子集中的用户名和图片 - How to also update user's name and image in comments sub-collection in Flutter via Firestore 在 flutter Firebase 中显示子集合 - Display sub-collection in flutter Firebase Flutter - 来自子集合的快照 - Flutter - Snapshot from Sub-collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM