I have a Collection
in Cloud Firestore that has a certain number of items, let's call it Collection
"X". This number of items will constantly be changing. At any given time, I want to listen for the number of items in this Collection
and create several whereEqualto()
calls on a Query
object that is based on another 'Collection', let's call it "Y":
Query queryStore = FirebaseFirestore.getInstance()
.collection("Y")
.whereEqualTo(USER_ID_LABEL, "item 1 from X")
.whereEqualTo(USER_ID_LABEL, "item 2 from X")
.whereEqualTo(USER_ID_LABEL, "item 3 from X");
//Could be more than 3, could be less than 3, constantly changing
Essentially, the number of whereEqualTo()
will be dynamic.
Is this type of query possible?
Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections.
Firestore is using websockets under the hood to react to changes, plus all the optimization based on lessons learned after the original firebase realtime db, it will scale! This is just a simple example and would require some restructuring and designing, but the ideas presented can and will scale.
Cloud Firestore also features richer, faster queries and scales further than the Realtime Database. Realtime Database is Firebase's original database. It's an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.
You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.
Yes. whereEqualTo() returns a Query object, which can be used to build up the final query you need. There's no obligation to chain the methods all at once, as you've demonstrated in your question. What you've written is equivalent to this:
Query queryStore = FirebaseFirestore.getInstance().collection("Y")
queryStore = queryStore.whereEqualTo(USER_ID_LABEL, "item 1 from X");
queryStore = queryStore.whereEqualTo(USER_ID_LABEL, "item 2 from X");
queryStore = queryStore.whereEqualTo(USER_ID_LABEL, "item 3 from X");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With