Shady Aziza
Shady Aziza

Reputation: 53307

Check Firestore data for the existence of each element in an array

I am trying to query on a single node that has the following structure:

//object in firestore    
fruit = {"Orange","Apple","Pineapple","Banana","Grapes"}

Now my logic is setup that I have an array, let's say we have two items in the array like this

//object in my code
fruit:any = ["Pineapple","Grapes"];

and I want to check if the items in the array exist under fruit in the database, I do the following query

    checkFruits(uid,fruit):Observable<any>{
        //console.log(fruit);
        return this.afs.collection('profiles').doc(uid).collection(`fruit`, ref => ref.
        //what to do here to make it check for each item in fruit array ??
        where(`fruit.${fruit}`, '==', true)).valueChanges(); 
         }

I cant quite wrap my head around how to to check the existence of each element in my array under fruit object.

Upvotes: 0

Views: 1229

Answers (1)

Shady Aziza
Shady Aziza

Reputation: 53307

I managed to do it this way:

return this.afs
      .collection("profiles")
      .doc(uid)
      .collection(`experiences`, ref => {
        let query: any = ref;
        //console.log(fruit);

        for (const iterator in fruit) {
          console.log(iterator);

          query = query.where(`fruit.${iterator}`, "==", true);
        }
        return query;
      })
      .valueChanges();

I got help from Hady and Mohamed

Upvotes: 2

Related Questions