Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JSON format for a firestore individual field index within a collection group query?

We can now retrieve documents from a collection group which is great. To do so, I need to create an index through an error message on the Firebase console. How can I add this new index to the firestore.indexes.json file?

Example of the documentation:

let museums = db.collectionGroup('landmarks').where('type', '==', 'museum');
museums.get().then(function(querySnapshot) {
  querySnapshot.forEach(function(doc) {
    console.log(doc.id, ' => ', doc.data());
  });
});
like image 493
Bertrand Canavaggio Avatar asked Oct 28 '25 01:10

Bertrand Canavaggio


1 Answers

At the top level of your index file, add a new element called fieldOverrides and populate it like this:

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

This preserves the all the default automatic indexing for the type field in landmarks at the COLLECTION scope, and allows for type also to be used at the COLLECTION_GROUP scope.

like image 98
Doug Stevenson Avatar answered Oct 30 '25 22:10

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!