0

I'm new in Firebase and I have many problems to do any simple database operation.
I need to filter a set of data using more attribute. This is my Firebase realtime database:

  "items" : {
    "-KuJdzzI82tnp1etnR0E:" : {
      "dateEnd" : 1506356341284,
      "dateStart" : 1505751541284,
      "enabled" : 0,
      "title" : "cat"
    },
    "-KuKeCwPtkH_Ub9YMq4C:" : {
      "dateEnd" : 1505665110444,
      "dateStart" : 1505146710444,
      "enabled" : 1,
      "title" : "wolf"
    },
    "-KuKeD1evhc4mw2njQqV:" : {
      "dateEnd" : 1506961268244,
      "dateStart" : 1506442868244,
      "enabled" : 0,
      "title" : "fish"
    }
  }

I have to extract element with title="wolf" if timestamp is between dateStart and dateEnd and if it's enabled="1".
I read in AngularFire2 doc that is possibile to querying list, but the only example is a single filter attribute with "orderByChild" and "equalTo" combination.
Is it possible using AngularFire2 to solve this problem ?
Thank you.

KENdi
  • 7,576
  • 2
  • 16
  • 31
enfix
  • 6,680
  • 12
  • 55
  • 80

1 Answers1

1

You cannot add two filters to the same query. You'll need to make a query on the expected smaller dataset, then filter out the results.

const results = []; // Results

this.db.list('/items', { query: {
    orderByChild: 'title',
    equalTo: 'wolf'
}}).subscribe(items => {
    const startTime = 0; // Some start time
    const endTime = 1; // Some end time

    items.forEach(item => {
        if (item.dateEnd < endTime && item.startTime > startTime) {
            results.push(item);
        }
    });

    // `results` will now be filled with the items.
})
sketchthat
  • 2,678
  • 2
  • 13
  • 22