This Content is from Stack Overflow. Question asked by Abby
I am trying to fetch chat data between two individuals from firestore database. let’s say the two individuals are
I am trying to fetch data which holds the below condition:
(to =='Chris' AND from == 'Sam') OR (to == 'Sam' AND from == 'Chris')
Please guide me on how I can use both OR and AND logic in my firestore query.
Firestore doesn’t support OR queries that way. You’ll have to run 2 different queries in Firestore:
const colRef = collection(db, "chats"); // Query for (to =='Chris' AND from == 'Sam') const q1 = query(colRef, where("to", "==", "Chris"), where("from", "==", "Sam")) // Query for (to == 'Sam' AND from == 'Chris') const q2 = query(colRef, where("to", "==", "Sam"), where("from", "==", "Chris"))
A workaround would be to store a key with value
SamChris) and use the
in operator like this:
const q3 = query(colRef, where("field", "in", ["ChrisSam", "SamChris"]))
However, this will require you to have unique user names across the applications (or use the User IDs instead). Also make sure the security rules only allow these 2 to query these documents.
This Question was asked in StackOverflow by Abby and Answered by Dharmaraj It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.