2

Is there a way to use array-contains-any and order the query by the items contained in the array? I know it can be done once the array is retrieved, but I'm planning to use pagination so I can retrieve thousands of documents by page.

2
  • I'm having a hard time visualizing what the ordering should look like. Can you edit your question to include some example documents, and what the result should look like. I doubt it is possible within the array-contains-any clause, but we might be able to come up with a workaround once we see more details. Commented Sep 2, 2021 at 23:36
  • 1
    I think I got what you mean after re-reading the title. I wrote an answer below. If thst is not what you meant, I still recommend updating the question with the info I asked for above. Commented Sep 3, 2021 at 0:04

1 Answer 1

1

Firestore can only filter on values that exist in the document. It can't perform calculations to get the value to filter on, as the value needs to exist in the index.

If you want to filter on the number of items that exist in an array field, you can add an additional field that keeps that count and then update that field whenever you update the array itself. Once you have this additional field, you can easily use it in query conditions.

If you want to order on the number of values in the array that were matched by the query, that is not possible in a query. You'll have to perform that sorting in your application after the query has returned its results.

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

Comments

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.