angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Query based on multiple where clauses

Open ohanspace opened this issue 10 years ago • 10 comments

Please help!!!!

{ 
"members" : 
      "uid1" : { "name" : "x", "bloodGroup" : "A+", "location" : "some Place"},
      "uid2" : { "name" : "y", "bloodGroup" : "A-", "location" : "some place"},
      ...
      ...
}

if I have thousands of member records like this. how could I filter them based on bloodGroup AND location and fetching say 100 records from server at a time using angularfire2. As I see querying list only support one parameter in orderByChild, equalTo. How could I solve this problem?

ohanspace avatar Jun 30 '16 15:06 ohanspace

I am trying to figure out the same thing. I have my list ordered in a certain way using orderByChild:'daysTillDue' and now i want to filter the same list based on a different child property, but still have that list ordered by daysTillDue. How should we go about something like this?

Adtiv avatar Jul 01 '16 01:07 Adtiv

Firebase doesn't allow querying by two children. So what you'll have to do is query by one child and then filter the results on your end. Firebase docs also suggests that you store your data the way you intend to query it, so maybe you need to think of a way to index those records.

idanen avatar Jul 01 '16 07:07 idanen

@idanen may be right..I didn't find any other way. But it would be great help for developer If the angularfire support this extra feature internally :(

ohanspace avatar Jul 01 '16 08:07 ohanspace

I believe this kind of "feature" will require creating lists above the actual "database", so what they're saying is create it yourself.

idanen avatar Jul 01 '16 08:07 idanen

You can only query by one field with the Realtime Database.

However you can restructure your data to help.

{
   "members": {
      "$bloodGroup": {
                }
   }
}

This way you can get items by

af.database.list('members/A+', {
  query: {
      orderBy: 'location',
      equalTo: 'some place'
   }
})

davideast avatar Jul 06 '16 23:07 davideast

@ohanspace , I know it is a bit late for an answer, but I encountered the same problem and I resolved it by creating a third index :

{ "members" : "uid1" : { "name" : "x", "bloodGroup" : "A+", "location" : "some Place", thirdIndex:"A+//some Place"}, "uid2" : { "name" : "y", "bloodGroup" : "A-", "location" : "some place", thirdIndex:"A-//some Place"}, ... ... }

This is not perfect, but like this you can keep doing the "querying" job by angularFire2 instead of filtering your records in in your front app.

af.database.list('members', { query: { orderBy: 'thirdIndex', equalTo: 'A+//some Place' } })

mquentin avatar Oct 31 '16 15:10 mquentin

I have just the same problem and I'll use the filter for now. But it would be very cool, if that would be possible some day, because it could really reduce the size of network transmissions.

cherry-wave avatar Feb 16 '17 12:02 cherry-wave

+1

raDiesle avatar Jul 31 '17 20:07 raDiesle

@mquentin commented on Oct 31, 2016: this solution is elegant and very userful

hiepxanh avatar Aug 20 '17 11:08 hiepxanh

Hello Everyone,

I would like to know about multiple query in angularfire2.

my code: const dcoclublist: AngularFireList = this.db.list('d', ref => ref.orderByChild('xname').equalTo(id)&&.orderByChild(yname).equalTo(true));

it gives me only data equal true and not both...

any help,it will be appreciated...

Thanks

MELAS007 avatar Feb 24 '18 11:02 MELAS007