Monu
Monu

Reputation: 907

Firestore - How to create a composite index on all entries of the map?

I have an android query:

Query query = firestore
    .collection("users")
    .orderBy("email")
    .whereEqualTo("packages." + pkg, true);

So I need to create a composite index including email and packages.`Package Name`.

But the problem is I need to create a new index every time I create a new Package.

How can I index over a complete map, So that it will automatically add an index on every entry in the package map and email?

I tried adding:

field 1 - email - Ascending
field 2 - packages - Ascending

But it didn't work

Is regex possible, like:

field 1 - email - Ascending
field 2 - packages.`.*`

Upvotes: 1

Views: 128

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317750

Instead of using map where the properties of the map are essentially unbounded and require lots of indexes, you should instead use a list field for this query. If you take the package strings and put them into list like this:

packages: (list)
  0: package1
  1: package2

You can query for existence of a package in that field like this using an array membership query:

Query query = firestore
    .collection("users")
    .orderBy("email")
    .whereArrayContains("packages", pkg);

Upvotes: 1

Related Questions