Query based on multiple where clauses in firebase -


{ "movies": {     "movie1": {         "genre": "comedy",         "name": "as gets",         "lead": "jack nicholson"     },     "movie2": {         "genre": "horror",         "name": "the shining",         "lead": "jack nicholson"     },     "movie3": {         "genre": "comedy",         "name": "the mask",         "lead": "jim carrey"     }   }    } 

i firebase newbie. how can retrieve result data above where genre = 'comedy' , lead = 'jack nicholson' ?

what options have? in advance.

using firebase's new query api, might tempted try this:

// !!! not work !!! ref   .orderby('genre')   .startat('comedy').endat('comedy')   .orderby('lead')                  // !!! line raise error !!!   .startat('jack nicholson').endat('jack nicholson')   .on('value', function(snapshot) {        console.log(snapshot.val());    }); 

but @robdimarco firebase says in comments:

multiple orderby() calls throw error

so my code above not work.

i know of 3 approaches work.

1. filter on server, rest on client

what can execute 1 orderby().startat()./endat() on server, pull down remaining data , filter in javascript code on client.

ref   .orderby('genre')   .equalto('comedy')   .on('child_added', function(snapshot) {        var movie = snapshot.val();       if (movie.lead == 'jack nicholson') {           console.log(movie);       }   }); 

2. add property combines values want filter on

if isn't enough, should consider modifying/expanding data allow use-case. example: stuff genre+lead single property use filter.

"movie1": {     "genre": "comedy",     "name": "as gets",     "lead": "jack nicholson",     "genre_lead": "comedy_jack nicholson" },... 

you're building own multi-column index way , can query with:

ref   .orderby('genre_lead')   .equalto('comedy_jack nicholson')   .on('child_added', function(snapshot) {        var movie = snapshot.val();       console.log(movie);   }); 

david east has written library called querybase helps generating such properties.

you relative/range queries, let's want allow querying movies category , year. you'd use data structure:

"movie1": {     "genre": "comedy",     "name": "as gets",     "lead": "jack nicholson",     "genre_year": "comedy_1997" },... 

and query comedies of 90s with:

ref   .orderby('genre_year')   .startat('comedy_1990')   .endat('comedy_2000')   .on('child_added', function(snapshot) {        var movie = snapshot.val();       console.log(movie);   }); 

this can work multiple values, can range filter on last value in composite property.

3. create custom index programmatically

yet alternative we've done before new query api added: create index in different node:

  "movies"       // same structure have today   "by_genre"       "comedy"           "by_lead"               "jack nicholson"                   "movie1"               "jim carrey"                   "movie3"       "horror"           "by_lead"               "jack nicholson"                   "movie2" 

there more approaches. example, answer highlights alternative tree-shaped custom index: https://stackoverflow.com/a/34105063


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -