How to get data by multiple values of one field? For example, I have database with posts and I want to query for all posts where blogId is 1 or 2, sorting by timestamp.
collection("posts").whereEqualTo("blogId", "1")
.whereEqualTo("blogId", 2).orderBy("timestamp", Query.Direction.DESCENDING).limit(50)
Code above is not working :(
How to achieve this? Regards :)
Firebase has listened to our requests and they have included
IN
query from7 Nov, 2019
. It's a kind ofOR
query, where you can have upto10 OR
filters.For android:
Firebase documentation
You could combine the Observables and return as one
You can use De Morgaan's laws to rewrite an
AND
query as anOR
query.I couldn't find any documentation for the ability to OR together where conditions. But you can rephrase your requirement on the
blogId
as follows:Try this code:
Firestore now supports "IN" queries for this purpose.
The query would look like this:
You can have up to 10 values (fieldValueX) to check "IN" of.
The code OP desired would be as follows:
OR
operator is not accepted in firebase firestore. Normally using firebase syntax you can call two collections:and you can merge the results before serving to the view.
But in this case where you have blogIds you can use this syntax:
collection("posts").orderBy('blogId').startAt(1).endAt(2);
e.d