3

I saw a similar question here: Is there a workaround for the Firebase Query "IN" Limit to 10?

The point now is, with the query in, the union works, but with the query not-in it will be intersection and give me all the documents, anyone knows how to do this?

3
  • 1
    The solution you'll need to use depends entirely on what you are querying and why you are querying for it. You can either tag each different document with a marker that signifies they are meant to be excluded and filter on that, or you can use a not-in query to handle the first 10 documents and then filter the remaining documents on the client-side. Commented Jan 26, 2022 at 5:20
  • In my app, I have a sort of filters where you can say what do you want to see and/or what you don't want to see, for the last option I have to retrieve all docs (could be easily more than 10k documents), and get charged for all those reads Commented Jan 26, 2022 at 19:03
  • 1
    Please provide some example data and an example query you are trying to do. Without it we can't tell if there is a better approach to what you are trying to do. Commented Jan 27, 2022 at 0:01

1 Answer 1

0

As @samthecodingman mentioned, it's hard to provide specific advice without examples / code, but I've had to deal with this a few times and there are a few generalized strategies you can take:

Restructure your data - There's no limit on the number of equality operators you can use You can use up to 100 equality operators, so one possible approach is to store your filters/tags as a map, for example:

id: 1234567890,
...
filters: {
    filter1: true,
    filter2: true,
    filter3: true,
}

If a doc doesn't have a particular tag, you could simply omit it, or you could set it to false, depending on your use case.

Note, however, that you may need to create composite indexes if you want to combine equality operators with inequality operators (see the docs). If you have too many filters, this will get unwieldy quickly.

Query everything and cache locally - As you mentioned, fetching all the data repeatedly can get expensive. But if it doesn't change too often or it isn't critical to get the changes in real time, you can cache it locally and refresh at some interval (hourly or daily, for example).

Implement Full-Text Search - If neither of the previous options will work for you, you can always implement full-text search using one of the services Firebase recommends like Elastic. These are typically far more efficient for use-cases with a high number of tags/filters, but obviously there's an upfront time cost for setup and potentially an ongoing monetary cost if your usage is higher than the free tiers these services offer.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a note, there is a limit on the number of equality operators you can have. "The sum of filters, sort orders, and parent document path (1 for a subcollection, 0 for a root collection) in a query cannot exceed 100." firebase.google.com/docs/firestore/query-data/…
Good catch @Ravendarksky! Updated the answer to reflect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.