简体   繁体   中英

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 . 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.

Dealsref basically creates a reference to every doc that I loop through with the 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. 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. 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());
    });
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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