簡體   English   中英

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

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

基本上,我的數據結構如下...

根集合=“企業”

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

我需要做的是遍歷每個企業擁有的每筆交易並檢查條件,如果滿足該條件則刪除該交易,這與問題無關。 我遇到的問題是在NODE JS中獲取子集合引用的子集合和文檔。 以下是到目前為止的內容。

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);
    });


        });
    });

}

在這里,businesssCopy =一個數組,其中填充了對根businesss集合中所有業務文檔的引用。

Dealsref基本上創建了對我使用forEach遍歷的每個文檔的引用。

在我做dealsRef.get()之后,對於每個循環我都在掙扎,它獲取了我循環通過的每個業務文檔的子集合。 在該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