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

后端 未结 9 1901
没有蜡笔的小新
没有蜡笔的小新 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:26

    This doesn't solve all cases, but for "enum" fields, you can emulate an "OR" query by making a separate boolean field for each enum-value, then adding a where("enum_", "==", false) for every value that isn't part of the "OR" clause you want.

    For example, consider your first desired query:

    1. Give me all documents where the field status is open OR upcoming

    You can accomplish this by splitting the status: string field into multiple boolean fields, one for each enum-value:

    status_open: bool
    status_upcoming: bool
    status_suspended: bool
    status_closed: bool
    

    To perform your "where status is open or upcoming" query, you then do this:

    where("status_suspended", "==", false).where("status_closed", "==", false)
    

    How does this work? Well, because it's an enum, you know one of the values must have true assigned. So if you can determine that all of the other values don't match for a given entry, then by deduction it must match one of the values you originally were looking for.

提交回复
热议问题