How to perform compound queries with logical OR in Cloud Firestore?

后端 未结 9 1936
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 02:07

From the docs:

You can also chain multiple where() methods to create more specific queries (logical AND).

How can I perform an <

9条回答
  •  庸人自扰
    2020-11-22 02:31

    We have the same problem just now, luckily the only possible values for ours are A,B,C,D (4) so we have to query for things like A||B, A||C, A||B||C, D, etc


    As of like a few months ago firebase supports a new query array-contains so what we do is make an array and we pre-process the OR values to the array

    if (a) {
    array addObject:@"a"
    }
    if (b) {
    array addObject:@"b"
    }
    if (a||b) {
    array addObject:@"a||b"
    }
    etc
    

    And we do this for all 4! values or however many combos there are.

    THEN we can simply check the query [document arrayContains:@"a||c"] or whatever type of condition we need.

    So if something only qualified for conditional A of our 4 conditionals (A,B,C,D) then its array would contain the following literal strings: @["A", "A||B", "A||C", "A||D", "A||B||C", "A||B||D", "A||C||D", "A||B||C||D"]

    Then for any of those OR combinations we can just search array-contains on whatever we may want (e.g. "A||C")


    Note: This is only a reasonable approach if you have a few number of possible values to compare OR with.

    More info on Array-contains here, since it's newish to firebase docs

提交回复
热议问题