0

My current Structures looks like this

Collection Posts

- PostID
       - titel
       - content
       - titelimgurl
       - tags
       - timestamp

Now getting the Data here is easy, what I wanna achieve is splitting up the Content from the Rest so that I dont have to fetch unneeded data on my frotnpage.

So I plan to go for this structure:

Collection Posts

- PostID
       - titel
       - titelimgurl
       - tags
       - timestamp

Collection Content

- PostID
       - content

The problem is that Im not sure how to retrieve the data efficiently there for example:

var postRef = db.collection("posts").doc(this.docID);

postRef.get().then(function(doc) {
    if (doc.exists) {
        var contentRef = db.collection("content").doc(doc.id)
        contentRef.get().then(function(doc) {
            ....
         }
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

This looks overly complicated specially when assigning this data to an actual collection of posts in my Store, any ideas appreciated on how I can make this more efficient.

niclas_4
  • 3,514
  • 1
  • 18
  • 49
  • That's pretty standard for Firestore and NoSQL in general, since there are no "join" operations. – Doug Stevenson Oct 26 '18 at 07:14
  • @DougStevenson Okay, but how do I know when its more efficient to Split the Collection up in different parts in terms of Cost. (More Reads vs Larger Reads) – niclas_4 Oct 26 '18 at 08:00
  • It's not really possible to say without knowing a lot about the data you're storing. If you want to know what's most efficient, you'll need to do some benchmarking and measuring of your data. – Doug Stevenson Oct 26 '18 at 09:03

1 Answers1

1

If you want to query the database only once, then you shouldn't denormalize your database by creating that content as a top level collection. You could keep the content of the post as a property within the PostID document. Unlike in Firebase real-time database, where to display a list of post objects, you would have been downloaded the entire posts node, in Cloud Firestore this is not an issue anymore. So you can feel free to keep the content as a property in order to make a single query.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Oh interesting that this is possible in Firestore, I have never heard of it yet, do you have any Documentation Reference for me to read into? Thanks in advance. – niclas_4 Oct 26 '18 at 07:41
  • I'm afraid I don't understand you. But you should go ahead with the first database schema, in which the content is a property within the post document. Using that option, only a read operation is involved. – Alex Mamo Oct 26 '18 at 07:58
  • Is there everything alright, have you solved the issue? – Alex Mamo Oct 29 '18 at 08:16
  • Hello, no unfortunatly I wasnt able to find out how I can query only specific parts of a Document. – niclas_4 Oct 29 '18 at 08:18
  • I understand now what you are looking for but unfortunately you cannot query only specific parts, it's the entire document, or nothing. Please take a look at my answer from this **[post](https://stackoverflow.com/questions/52295327/how-to-get-the-modified-field-or-data-from-the-doc-firebase-firestore-realtime-u/52295562#52295562)** for a better understanding. – Alex Mamo Oct 30 '18 at 02:54
  • Yep that was also why I was confused about your initial answer, I was sure there is no way to query it like that. I think I have to check what is more worth in terms of cost, 2 Reads (splitting content from the post doc in a own collection) or reading the whole content of every page. Thanks for your help so far. – niclas_4 Oct 30 '18 at 06:28