0

I have a collection of documents, where each document has an attribute called "position" that can take one of the possible String values: A, B, C, D, E ... (from an ENUM that controls the object state machine )

How can I create a query in firestore to return documents whose value for the "position" attribute is "A" or "D"?

In Firestore documentation it is possible to understand that there is no implementation of the difference logical operator. Compound Queries only meet the "AND" condition on a single ATTRIBUTE value.

In this particular case, I cannot change attribute type because in future other elements may be added and cause requirements problems if I work with a numeric range.

I thought of making two queries and merge the result. But it would be horrible to do that with pagination and complex queries (doesn't work). In addition to running two queries, the more conditions I need to adjust in the future, the more code maintenance and more queries! right?

Is there any way to solve this problem?

Mateus
  • 389
  • 3
  • 20
  • Unfortunately, that's the way you should go ahead with right now, by creating two queries and merge the result client side. – Alex Mamo Sep 19 '19 at 16:37
  • @AlexMamo i saw your question, but, although it is the "solution" offered. It does not work in practice for much combination of criterias. Especially if the results need to be "pagination" at runtime with Firebase UI Firestore or custom adapters etc. – Mateus Sep 19 '19 at 16:41
  • I know that, so hope that Firebase team will release that feature sooner. – Alex Mamo Sep 19 '19 at 16:49

1 Answers1

0

Logical OR queries. In this case, you should create a separate query for each OR condition and merge the query results in your app.

source: https://firebase.google.com/docs/firestore/query-data/queries#compound_queries

Flignats
  • 1,234
  • 10
  • 21
  • 1
    Thanks for the contribution! I edited the question to make it clearer. Working with N queries and using pagination is not that simple. It would be interesting to have some other solution directly in the query. Managing N collections with pagination is really unfeasible in most cases. – Mateus Sep 19 '19 at 16:45
  • From your additional comments, I'm thinking that the data needs to be normalized in order to achieve the function and efficiency you're wanting. When queries start becoming too complex, it is usually an indication of normalization being needed – Flignats Sep 19 '19 at 16:50
  • Really? not at all. I'm come back to mongo atlas. – Mateus Sep 20 '19 at 20:34