Home > other >  How to use array contains in 2 different fields or is there an alternative query?
How to use array contains in 2 different fields or is there an alternative query?

Time:01-08

I want filter languages and id, but I can't use it because it is in different fields. What can i do differently to allow this use-case?

eg. my values:

languages = ["English","Türkçe"]
passedUserIds = ["asasd123123","12312asd12dsa"]
smashedUserIds = ["abcvb123123","1231cvb12dsa"]

Limitations: Note the following limitations for != queries: Only documents where the given field exists can match the query. You can't combine not-in and != in a compound query. In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.

and second:

Limitations: Note the following limitations for in, not-in, and array-contains-any: in, not-in, and array-contains-any support up to 10 comparison values. You can use at most one array-contains clause per query. You can't combine array-contains with array-contains-any. You can use at most one in, not-in, or array-contains-any clause per query. You can't combine these operators in the same query. You can't combine not-in with not equals !=. You can't order your query by a field included in an equality (==) or in clause.

And my firestore model:

collections     
matches     
passes      
smashes     
users     
     =>documents    
             ids eg         
         1234214asdf12          
          1231212abd              
               => fields              
                     gender                    
                  "Female"                     
                      id              
                        "1231212abd"         
                      languages                  
                       0                   
                    "中文"                
                        1                
                      "عربي"                
                          2                  
                   "Deutsch"     

My code:

 unlanguages = firestore().collection("users")
.where("languages", "array-contains-any", languages)
.where("id", "not-in", [...passedUserIds, ...smashesUserIds]) .onSnapshot(querySnapshot =>{  
if (querySnapshot?.docs?.length > 0) {    
    setProfiles(querySnapshot?.docs.filter((doc) => doc.id != user.uid)
    .filter((doc) => doc._data.gender != loggedInProfile.gender)
    .filter((doc) => doc._data.age <= ${value[1]})
    .filter((doc) => doc._data.age >= ${value[0]}) 
    .map((doc) => ({ id: doc.id, ...doc.data() })))
    setActiveLoad(false);     
    } else {
    setActiveLoad(false);
    }
    })    
    return unlanguages

and i tried many things in the

setProfiles(querySnapshot?.docs .filter((doc) => doc.id != user.uid)    
=> .filter((doc) => doc.id != passedUserIds)    
=> .filter((doc) => doc.id != smashesUserIds)    
 .filter((doc) => doc._data.gender != loggedInProfile.gender)   
 .filter((doc) => doc._data.age <= ${value[1]})    
 .filter((doc) => doc._data.age >= ${value[0]})    
 .map((doc) => ({ id: doc.id, ...doc.data() })))

I added but not working and if I use just like this

passedUserIds[0]

yes its working but only 1 value. so It's could be 1000 maybe 10000 value, I do not know what to do?

CodePudding user response:

i solved it like this, i gave the opportunity to choose only 1 language while filtering, dropdownpicker, I set multiple false, and its working

and last filter codes

.where("id", "not-in", [...passedUserIds, ...smashesUserIds])
        .onSnapshot(async querySnapshot => {
            if (querySnapshot?.docs?.length > 0) {
                setActiveLoad(true)
                let dondur2 = await querySnapshot?.docs
                    .filter((doc) => doc.id != user.uid)
                    .filter((doc) => doc._data.gender != loggedInProfile.gender)
                    .filter((doc) => doc._data.age <= `${value[1]}`)
                    .filter((doc) => doc._data.age >= `${value[0]}`)
                    .filter((doc) =>
                        doc._data.languages[0] == languages ||
                        doc._data.languages[1] == languages ||
                        doc._data.languages[2] == languages ||
                        doc._data.languages[3] == languages ||
                        doc._data.languages[4] == languages ||
                        doc._data.languages[5] == languages ||
                        doc._data.languages[6] == languages ||
                        doc._data.languages[7] == languages
                    )
                    .map((doc) => ({ id: doc.id, ...doc.data() }))
  • Related