IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

Is there any way to query with multiple Ids from a collection in single query? #82

Closed anjali198 closed 4 years ago

anjali198 commented 4 years ago

I want to query with multiple Ids or multiple where conditions from a collection in a single query, please help me with it if anyone has worked on it? While querying more than once for the same condition every time getting the different collection in the result and also not able to merge those collections to show it on the same page.

damonmaria commented 4 years ago

Firestorter can only do what Firestore can do. My understanding is you're looking to provide multiple IDs to search for returning all that match. This would need an OR condition which Firestore does not support.

However, you could easily mimic this behavior by using a Mobx computed property that combined multiple queries or Document instances.

IjzerenHein commented 4 years ago

Hi, @damonmaria has already provided the correct answer. Google Firestore in its-self cannot query on multiple Ids. If you want to do that then you will need to define multiple queries in the client-side and aggregate the results yourself.

thdk commented 4 years ago
function getMany(collectionRef, ...ids) {
 return Promise.all(ids.map(id => collectionRef.doc(id).get());
}

getMany("id1", "id2") // or getMany(...["id1", "id2"])
    .then(([result1, result2]) => {
         // result1.data().foo
         // result2.data().foo
    }