Afsara
Afsara

Reputation: 1461

Firebase collection query on multiple documents check

Is it possible in firebase firestore to get information about the multiple documents are available or using where query we are using react native.

userRef.where('number', '==', '123')
    .where('number', '==', '1234')
    .get()
    .then(r =>{

      r.forEach(n => console.log(n))

    });

is it possible to get those two documents in response ?

if I use like single where its working fine, how can I use same thing for nested documents compare ?

Upvotes: 1

Views: 1306

Answers (1)

Kim
Kim

Reputation: 1198

If I understand your use-case correctly, you want to get exactly two users, where user one has the number 123 and user two has the number 1234.

Such query to my understanding is currently not possible with Firestore, but you can split them up into two queries and merge the result.

const userOne = userRef.where('number', '==', '123').get();
const userTwo = userRef.where('number', '==', '1234').get();

Promise.all([userOne, userTwo])
.then(result => {
    /*
    * expected output: Array [QuerySnapshot, QuerySnapshot]
    * First QuerySnapshot is result from userOne "where"
    * Second QuerySnapshot is result from UserTwo "where"
    */

    const userOneResult = result[0];
    const userTwoResult = result[1];

    if (userOneResult.empty === false && userTwoResult.empty === false) {
        // Get both your users here
    }
})

Upvotes: 4

Related Questions