What is the JSON format for a firestore composite index including an array?

Viewed 5511

I want to filter a collection (let's call it documents) using array-contains on one column (say keywords) and sort by another column (say name).

I am able to create this composite index in the firebase console, but I can only guess at the format for adding it to firestore.indexes.json.

It's unfortunate we can't download the index file from the console.

3 Answers

Set the mode to ARRAY_CONTAINS:

{
      "collectionId": "documents",
      "fields": [
        {
          "fieldPath": "keywords",
          "mode": "ARRAY_CONTAINS"
        },
        {
          "fieldPath": "name",
          "mode": "ASCENDING"
        }
  ]
}

You can also list your current Cloud Firestore indexes in JSON from the Firebase CLi:

firebase firestore:indexes

To export firestore indexes

firebase firestore:indexes > firestore.indexes.json

Caution! This will replace everything in firestore.indexes.json.

Or if you have multiple firebase projects in your .firebaserc:

"projects": {
    "production": "prod-v6909",
    "staging": "prod-c6020"
}

Then you can export like this:

firebase --project staging firestore:indexes > firestore.indexes.json

You can also list your current Cloud Firestore indexes in JSON from the Firebase CLi: firebase firestore:indexes

For me this when exporting collection group indexes this is exporting a bad formatter json. The command line forget to close the array notifier on indexes:

{
  "indexes": [],
  "fieldOverrides": [
    {
      "collectionGroup": "restaurants_users_config",
      "fieldPath": "user",
      "indexes": [
        {
          "order": "ASCENDING",
          "queryScope": "COLLECTION"
        },
        {
          "order": "DESCENDING",
          "queryScope": "COLLECTION"
        },
        {
          "arrayConfig": "CONTAINS",
          "queryScope": "COLLECTION"
        },
        {
          "order": "ASCENDING",
          "queryScope": "COLLECTION_GROUP"
        }
    }
  ]
}

Any Suggestions? It is right to only fix the json array by hand?

**edit Fix: it was fixed with firebase upgrade 8.2.0 → 8.4.1

Related