Skip to content Skip to sidebar Skip to footer

Using "array-contains" Query For Cloud Firestore Social Media Structure

I have a data structure that consists of a collection, called 'Polls.' 'Polls' has several documents that have randomly generated ID's. Within those documents, there is an addition

Solution 1:

Would the "array_contains" query be another practical option for social media structure scalability?

Yes of course. This the reason why Firebase creators added this feature.

Seeing your structure, I think you can give it a try, but to responde to your question.

What are the limitations on the "array_contains" query?

There is no limitations regarding what type of data do you store.

Is it practical to have an array stored in Firebase that contains thousands of users / followers?

Is not about practical or not, is about other type of limitations. The problem is that the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:

Maximum size for a document: 1 MiB (1,048,576 bytes)

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much. So in your case, if you would store only ids, I think that will be no problem. But IMHO, as your array getts bigger, be careful about this limitation.

If you are storing large amount of data in arrays and those arrays should be updated by lots of users, there is another limitation that you need to take care of. So you are limited to 1 write per second on every document. So if you have a situation in which a lot of users al all trying to write/update data to the same documents all at once, you might start to see some of this writes to fail. So, be careful about this limitation too.

Solution 2:

I did a real-time polls system, here is my implementation:

I made a polls collection where each document has a unique identifier, a title and an array of answers.

Also, each document has a subcollection called answers where each answer has a title and the total of distributed counters in their own shards subcollection.

Example :

polls/
  [pollID]
    - title: 'Some poll'
    - answers: ['yolo' ...]
    answers/
      [answerID]
        - title: 'yolo'
        - num_shards: 2
        shards/
          [1]
            - count: 2
          [2]
            - count: 16

I made another collection called votes where each document is a composite key of userId_pollId so I can keep tracking if the user has already voted a poll. Each document holds the pollId, the userId, the answerId...

When a document is created, I trigger a Cloud Function that grab the pollId and the answerId and I increment a random shard counter in this answerId's shards subcollection, using a transaction.

Finaly, on the client-side, I reduce the count value of each shards of each answers of a poll to calculate the total.

For the following stuff, you can do the same thing using a middle-man collection called "following", where each document is a composite key of userAid_userBid so you can track easily which user is following another user without breaking firestore's limits.

Post a Comment for "Using "array-contains" Query For Cloud Firestore Social Media Structure"