Is there a way in angularfire to request compliance with the AND condition

How can we request a firebase from corner fire to meet two conditions. I wanted to search below two conditions, since AND matches

  • if your key is red
  • If your key has hex categories

or what will be the check in firebase, for example .indexOn or .validate

json is like

  {

   "color": {
     "-UqPNlZ8ddgdGMFSsD": {
       "color": "red",
       "categories" : {
         "hex" : {
           "subcategory": ["# 111", "# 333"]
         }
       },
       "status": "ok"
     },
     "-U9P4pBpYiId3ID64K": {
       "color": "blue",
       "categories" : {
         "hex" : {
           "subcategory": ["#ddd", "#eee"]
         }
       },
       "status": "ok"
     },
     "-U9UgOdffZdfdbSydF": {
       "color": "green",
       "categories" : {
         "hex" : {
           "subcategory": ["# 333", "# 555"]
         }
       },
       "status": "ok"
     }
 }
0
firebase angularfire
source share
1 answer

Currently, a Firebase request contains only one call to orderBy... They can also request only those properties that are at the same level below the returned nodes.

Thus, you cannot implement your requirements in the way you probably originally hoped:

 // THIS IS AN EXAMPLE THAT WILL NOT WORK ref.child('color') .orderByChild('color').equalTo('red') .orderByChild('color/categories').equalTo('hex'); 

Approach 1: add a new property that combines the properties you want to filter on

In some cases, you may want to get away with a top-level property view that combines the values ​​you want to filter. So let your color be in only one category. Then you can add an additional colorcategory property:

  {

   "color": {
     "-UqPNlZ8ddgdGMFSsD": {
       "color": "red",
       "colorcategory": "redhex",
       "categories" : {
         "hex" : {
           "subcategory": ["# 111", "# 333"]
         }
       },
       "status": "ok"
     },
     "-U9P4pBpYiId3ID64K": {
       "color": "blue",
       "colorcategory": "bluehex",
       "categories" : {
         "hex" : {
           "subcategory": ["#ddd", "#eee"]
         }
       },
       "status": "ok"
     },
     "-U9UgOdffZdfdbSydF": {
       "color": "green",
       "colorcategory": "greenhex",
       "categories" : {
         "hex" : {
           "subcategory": ["# 333", "# 555"]
         }
       },
       "status": "ok"
     }
 }

And filter it out:

 ref.child('color') .orderByChild('colorcategory').equalTo('redhex') 

This will work if you have two properties with the same value, even if they are at different levels of the JSON structure, since you normalize these properties and the level in one value at the right level.

But since your categories are ambiguous, this approach will not work either. The only approach I can think of is that you create a so-called secondary index for your data.

Approach 2: create a secondary index for the properties you want to filter on

An index in a NoSQL database is a data structure very similar to an index with several columns in a relational database, which exists only for your queries. In this case, since you want to request a combination of color and category, I would expect the color_category index color_category or perhaps even color_category_subcategory .

 color_category_subcategory: "red_hex_111": "-UqPNlZ8ddgdGMFSsD" "red_hex_333": "-UqPNlZ8ddgdGMFSsD" "blue_hex_ddd": "-U9P4pBpYiId3ID64K" "blue_hex_eee": "-U9P4pBpYiId3ID64K" "green_hex_333": "-U9UgOdffZdfdbSydF" "green_hex_555": "-U9UgOdffZdfdbSydF" 

You will need to create and maintain this index from your own code, either in the client application, or when starting the process on the server side that creates it. But as soon as you have an index, you can query for it:

 ref.child('color_category_subcategory') .orderByKey().equalTo('red_hex_333') .on('value', function(snapshot) {... 

Or by range:

 ref.child('color_category_subcategory') .orderByKey().startAt('blue_hex').endAt('blue_hex~') .on('value', function(snapshot) {... 

~ above is not a special operator, but simply a character that falls out after all other characters in an ASCII sequence. Thus, filtering ['blue_hex', 'blue_hex~'] will give all blue hexes.

+4
source share

All Articles