How to add subcollection to a document in Firebase Cloud Firestore

后端 未结 5 1131
一生所求
一生所求 2020-12-08 07:27

The documentation does not have any examples on how to add a subcollection to a document. I know how to add document to a collection and how to add data to a document, but h

5条回答
  •  执笔经年
    2020-12-08 08:03

    Here's my code:

    firebase.firestore().collection($scope.longLanguage + 'Words').doc($scope.word).set(wordData)
      .then(function() {
        console.log("Collection added to Firestore!");
        var promises = [];
        promises.push(firebase.firestore().collection($scope.longLanguage + 'Words').doc($scope.word).collection('AudioSources').doc($scope.accentDialect).set(accentDialectObject));
        promises.push(firebase.firestore().collection($scope.longLanguage + 'Words').doc($scope.word).collection('FunFacts').doc($scope.longLanguage).set(funFactObject));
        promises.push(firebase.firestore().collection($scope.longLanguage + 'Words').doc($scope.word).collection('Translations').doc($scope.translationLongLanguage).set(translationObject));
        Promise.all(promises).then(function() {
          console.log("All subcollections were added!");
        })
        .catch(function(error){
          console.log("Error adding subcollections to Firestore: " + error);
        });
      })
      .catch(function(error){
        console.log("Error adding document to Firestore: " + error);
      });
    

    This makes a collection EnglishWords, which has a document of. The document of has three subcollections: AudioSources (recordings of the word in American and British accents), FunFacts, and Translations. The subcollection Translations has one document: Spanish. The Spanish document has three key-value pairs, telling you that 'de' is the Spanish translation of 'of'.

    The first line of the code creates the collection EnglishWords. We wait for the promise to resolve with .then, and then we create the three subcollections. Promise.all tells us when all three subcollections are set.

    IMHO, I use arrays in Firestore when the entire array is uploaded and downloaded together, i.e., I don't need to access individual elements. For example, an array of the letters of the word 'of' would be ['o', 'f']. The user can ask, "How do I spell 'of'?" The user isn't going to ask, "What's the second letter in 'of'?"

    I use collections when I need to access individual elements, a.k.a. documents. With the older Firebase Realtime Database, I had to download arrays and then iterate through the arrays with forEach to get the element I wanted. This was a lot of code, and with a deep data structure and/or large arrays I was downloading tons of data that I didn't need, and slowing my app running forEach loops on large arrays. Firestore puts the iterators in the database, on their end, so that I can request a single element and it sends me just that element, saving me bandwidth and making my app run faster. This might not matter for a web app, if your computer has a broadband connection, but for mobile apps with poor data connections and slow devices this is important.

    Here are two pictures of my Firestore:

提交回复
热议问题