Home > OS >  Optimize index on query firestore
Optimize index on query firestore

Time:06-19

I'm close to exceed the quota limit index of firebase. So i'm trying to optimize it. At the moment, im using a tricks with object to get it with filter. Example: My query:

query = query.where(`genres.${genre}`, '==', true);
query = query.where(`languages.${lang}`, '==', true)

And my object:

[
  {
    id: 1,
    genres: {
      drama: true,
      comedy: true
    },
    languages: {
      en: true,
  },
  {
    id: 2,
    genres: {
      animation: true,
      action: true,
      comedy: true,
      drama: true,
    },
    languages: {
      en: true,
      fr: true,
    },
  }
]

I didnt know the number of entries can have genres or languages. So for every case i create an new index. Im close of 200 index.

I have tried to use the comparator '==' like this:

const genresSelected = ['drama', 'comedy'];
let genreObject = {};
for (let genre of genresSelected) {
  genreObject[genre] = true;
  query = query.where('genres', '==', genreObject);
}

So that will filter by genre === { drama: true, 'comedy': true } that will find my first item, but not my 2nd item. I would like get if property exist or not in genres / languages, not a strict check.

CodePudding user response:

I'd recommend adding an array field with all the present genres:

presentGenres: ['animation', 'action', 'comedy', 'drama']

With that new field you can then use array-contains to query for a single specific value:

query.where('presentGenres', 'array-contains', 'drama')

This

  • Related