Firestore: how to perform a query with inequality / not equals

后端 未结 7 1666
南笙
南笙 2020-11-22 05:05

I want select from Firestore collection just articles written NOT by me.
Is it really so hard?

Every article has field "owner_uid".

7条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 05:45

    EDIT Sep 18 2020

    The Firebase release notes suggest there are now not-in and != queries. (Proper documentation is now available.)

    • not-in finds documents where a specified field’s value is not in a specified array.
    • != finds documents where a specified field's value does not equal the specified value.

    Neither query operator will match documents where the specified field is not present. Be sure the see the documentation for the syntax for your language.

    ORIGINAL ANSWER

    Firestore doesn't provide inequality checks. According to the documentation:

    The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, or >=.

    Inequality operations don't scale like other operations that use an index. Firestore indexes are good for range queries. With this type of index, for an inequality query, the backend would still have to scan every document in the collection in order to come up with results, and that's extremely bad for performance when the number of documents grows large.

    If you need to filter your results to remove particular items, you can still do that locally.

    You also have the option of using multiple queries to exclude a distinct value. Something like this, if you want everything except 12. Query for value < 12, then query for value > 12, then merge the results in the client.

提交回复
热议问题