Firestore get documents where value not in array?

后端 未结 4 923
無奈伤痛
無奈伤痛 2020-11-28 15:02

Is there a way to get all documents which array field does not contain one or more values, now there is \"array-contains\" but is there something like \"array-not-contains\"

相关标签:
4条回答
  • 2020-11-28 15:31

    Considering that your collection will have a low number of documents, you could store all of their ids in another document using an onCreate cloud function trigger, download this document from the client and do the filtering client-side. You could also do all of this inside a cloud function if you're worried about performance.

    You'll have 1 extra read but that's no big deal, each document can have up to 1 MB of storage and that's a lot so you shouldn't be worried about it too much, you could also divide those ids into different documents and merge them on the client/cloud function if they get too big.

    This works very well for small sets of data, but if you're expecting millions of documents, then there isn't much you can do.

    0 讨论(0)
  • 2020-11-28 15:32

    I don't think that is possible at the moment. I would try looking at this blog post for reference.

    better arrays in cloud firestore

    You might need to convert your array to an object so that you can query by (property === false)

    0 讨论(0)
  • 2020-11-28 15:40

    Firestore recently added support for a not-in clause.

    citiesRef.where('country', 'not-in', ['USA', 'Japan']);

    That will get every doc where country exists and has a value other than 'USA', 'Japan', or 'null'.

    0 讨论(0)
  • 2020-11-28 15:43

    You can only query Firestore based on indexes, so that queries all scale up to search billions of documents without performance problems.

    Indexes work by recording values that exist in your data set. An index can't possibly be efficient if it tracks things that don't exist. This is because the universe of non-existant values compared to your data set is extremely large and can't be indexed as such. Querying for non-existence of some value would require a scan of all your documents, and that doesn't scale.

    0 讨论(0)
提交回复
热议问题