skip to Main Content

In a Firestore collection, I’m keeping logs under the activity document.

I’m considering two options here:

  1. Create a log collection separate from activities and keep logs here. Something like this image below:
    enter image description here

Maybe I can create a log collection under each activity too.

  1. Create a list under each activity document and keep the logs here as a map. Something like this image below:

a firebase collection on cloud firestore

At first glance, I think I would do less document reading with option 2 which is good for Firebase billing. I’m thinking if is this a good practice for Firestore collections.

Is option 1 really help me with the billing?
How would I know which one is better for billing or any other situation?

2

Answers


  1. Firestore is optimized for storing large collections of small documents. When querying, that’s the best option you have. For storing logs, the second option sounds like a good solution to me, but only as long as you keep the size of the documents under the maximum limit of 1 MiB. So in your case, I would choose to work with the second solution. So according to how often you would like to store the logs, you might consider using a document of logs for each month, a document for each day, or a document for each hour. It is up to you to choose the granularity. That being said, the second solution will be much cheaper than the first one.

    Login or Signup to reply.
  2. As usual when it comes to data modeling in NoSQL databases: it depends. Specifically, it depends on the use-cases of your app.

    Storing the logs inside the activity document, means that you always will read the logs when you read the activity document. If that is usually what you need in the app, then that’s great 👍. If you actually only need the logs for a small subset of the use-cases, then always loading them with the activity is wasteful.

    Similarly, storing the logs inside the activity makes it harder to query those individual logs. You’ll more often bump into query limitations with this model. But on the other hand, it does mean that querying the logs will actually return the activity, which may be what you want 👍 – or not of course.

    In the end, you will need to pick the data model that best meets all your use-cases and other criteria (such as the cost that Alex also mentions). This is usually not a one-time choice, but something that evolves as the use-cases of your app evolve. If you’re new to NoSQL data modeling, I’d recommend reading NoSQL data modeling and watching Get to know Cloud Firestore.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search