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

tccpg288 :

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 additional collection set called "answers." Users vote on these polls, with the votes all written to the "answers" subcollection. I use the .runTransaction() method on the "answers" node, with the idea that this subscollection (for any given poll) is constantly being updated and written to by users.

I have been reading about social media structure for Firestore. However, I recently came across a new feature for Firestore, the "array_contains" query option.

While the post references above discusses a "following" feed for social media structure, I had a different idea in mind. I envision users writing (voting) to my main poll node, therefore creating another "following" node and also having users write to this node to update poll vote counts (using a cloud function) seems horribly inefficient since I would have to constantly be copying from the main node, where votes are being counted.

Would the "array_contains" query be another practical option for social media structure scalability? My thought is:

  1. If user A follows user B, write to a direct array child in my "Users" node called "followers."
  2. Before any poll is created by user B, user's B's device reads "followers" array from Firestore to gain a list of all users following and populates them in the client side, in an Array object
  3. Then, when user B writes a new poll, add that "followers" array to the poll, so each new poll from user B will have an array attached to it that contains all ID's of the users following.

What are the limitations on the "array_contains" query? Is it practical to have an array stored in Firebase that contains thousands of users / followers?

enter image description here enter image description here

Alex Mamo :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=131502&siteId=1