简体   繁体   English

如何在Firestore中获取子集合引用[Node JS]

[英]How to get subcollection references in Firestore [Node JS]

Basically, my data structure is as follows ... 基本上,我的数据结构如下...

Root Collection = "Businesses" 根集合=“企业”

 -Docs of "Businesses" = various restaurant names For every business doc, there is a deals subcollection For every deals subcollection, there are documents of deals 

What I need to do is loop through every deal that every business has and check a condition and delete that deal if that condition is met, which is irrelevant to the question. 我需要做的是遍历每个企业拥有的每笔交易并检查条件,如果满足该条件则删除该交易,这与问题无关。 The problem I am having is fetching the subcollection and docs of subcollection references in NODE JS . 我遇到的问题是在NODE JS中获取子集合引用的子集合和文档。 Below is what I have so far. 以下是到目前为止的内容。

function delDeals()
{

businessesCopy.forEach(element)

{
    var dealsRef = db.collection('businesses').doc(element);

    dealsRef.getCollections().then(collections => {
        collections.forEach(collection => {

            var foodRef = db.collection('businesses').doc(element);
            var query = citiesRef.where('capital', '==', true).get()
            .then(snapshot => {
            snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
            });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    });


        });
    });

}

Here, businessesCopy = an array filled with references to all the business documents in the root businessess collections. 在这里,businesssCopy =一个数组,其中填充了对根businesss集合中所有业务文档的引用。

Dealsref basically creates a reference to every doc that I loop through with the forEach . Dealsref基本上创建了对我使用forEach遍历的每个文档的引用。

I am struggling in that for each loop right after I do dealsRef.get() , which fetches the subcollections of every business doc I loop through. 在我做dealsRef.get()之后,对于每个循环我都在挣扎,它获取了我循环通过的每个业务文档的子集合。 Inside that for loop, what I want to do is be able to fetch all the subcollections of every business doc, and then be able to loop through every document (Deal) of every deal subcollection of every business and delete that deal doc if it meets a certain condition. 在该for循环内,我想做的是能够获取每个业务文档的所有子集合,然后能够遍历每个业务的每个交易子集合的每个文档(交易),并在满足条件时删除该交易文档一定条件 I am not exactly sure how to get a reference to those deal docs of the deals subcollection for every business. 我不确定如何为每个企业获取交易子集合的那些交易文档的引用。

I think you're looking for: 我认为您正在寻找:

dealsRef.getCollections().then(collections => {
  collections.forEach(collection => {
    collection.where('capital', '==', true).get().then({              
      snapshot.forEach(doc => {
      console.log(doc.id, '=>', doc.data());
    });
  })
})

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

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