0

I have the following JSON stored on my local which I want to store it on Firebase Firestore:

guides: [  
    {  
       "id":0
       "name":"name0",
       "sources":[
           {
               "type":"s3",
               "url":"https://s3.amazonaws.com/xxxx/file0.mp3"
           }
       ]
    },
    {  
       "id":1
       "name":"name1",
       "sources":[
           {
               "type":"s3",
               "url":"https://s3.amazonaws.com/xxxx/file1.mp3"
           }
       ]
    }
]

What is the best solution for store "sources"? so when I make a search for "guides" (using firebase cloud functions), it retrieves the source list as well without making different searchs for each element of sources.

In Firebase Firestore, the array type doesn't allow a list of objects and I tried with "reference" but it returns the "structure and settings" of the document which is referencing.

function getGuides(guideId,response){
  db.collection('guides')
.where('id', '==', guideId).get()
.then(snapshot => {
    let guideDoc = snapshot.docs.map( doc => {
        return doc.data()
        })
    return guideDoc;
})
1
  • For a given guideare they several sources or only one? Commented Jan 15, 2019 at 19:17

1 Answer 1

2

In your answer, you say that "in Firebase Firestore, the array type doesn't allow a list of objects".

This is not correct: you can store objects in an array. With the Firebase console, you have to first select the array data type and then for each array member, select the map type. With the JavaScript SDK you can save a document that contains an array of objects.


Based on the above, you could adopt the following approach when using Firestore:

  • Have a collection of guide documents
  • For each guide document in this collection:
    • use the id value as the document ID;
    • have a field sources of type array in which you store objects like the one you show in your question.

This way you can query your guides by document id and get the array as follows:

var docRef = db.collection("guides").doc(guideId);

docRef.get().then(doc => {
    if (doc.exists) {
        const sourcesArray = doc.data().sources;
        sourcesArray.forEach((element) => {
            console.log(element.type); 
            console.log(element.url); 
        });
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, my problem was that I didn't know Firestore can store objects in an array.
@JoseAntonio Happy that I could help! I have updated my answer to put the point of array storage at the top. If you think my answer was helpful,you may also upvote it, in addition to your acceptance. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.