简体   繁体   中英

Firebase firestore query for two arrays

I have the next data structure in firestore:

{
  "votes_yes":[
    "id1",
    "id2",
    "id3",
    ...
  ],
  "votes_no":[
    "id4",
    "id5",
    "id6",
    ...
  ],
}

I want to build a query, which will return me a list of documents where the user has voted. To do that I need to check if his id exists in one of those arrays.

I can easily do it for one array with help of array-contains :

ref.where('votes_yes', 'array-contains', `userId`).get()

But is it possible to do logical OR for array-contains ?

I didn't find anything useful in the doc ... :(

The doc you linked to tells you what you need to know:

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.

I bolded the limitation that prevents you from using more than one array-contains per query. On top of that, Firestore doesn't support logical OR between multiple different fields , no matter what kind of query you're doing.

The only way to do what you're asking is to perform multiple queries and combine the results in your app using your own logic to determine which documents satisfy both conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM