angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.7k stars 2.19k forks source link

Query based on multiple where clauses #305

Closed ohanspace closed 8 years ago

ohanspace commented 8 years ago

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?

Adtiv commented 8 years ago

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?

idanen commented 8 years ago

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.

ohanspace commented 8 years ago

@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 :(

idanen commented 8 years ago

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

davideast commented 8 years ago

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'
   }
})
mquentin commented 8 years ago

@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' } })

cherry-wave commented 7 years ago

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.

raDiesle commented 7 years ago

+1

hiepxanh commented 7 years ago

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

MELAS007 commented 6 years ago

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