javascript - Firebase: How do I retrieve records from my data for which a specific key exists? -


i have data in firebase looks this:

"application": {   "companies": {     "firebase": {       "creation": {         "name": "firebase inc",         "location": "usa"       },        "google": {         "creattion": {           "name": "google inc",           "location": "usa"         }       }        "facebook": {       },        "apple": {       }     }   } } 

there tens of thousands of records under companies key. how efficiently execute following queries?

how query records key creation present under name?

how query records not have key creation present under name?

i want call .on('child_added') on returned result set can process specific records later on. possible?

edit: simpler way without using parameter

queries

here queries without having use parameter:

  • find companies without creation:
    • var ref = new firebase(fburl+'/companies').orderbychild("creation").equalto(null);
  • find companies with creation:
    • var ref = new firebase(fburl+'/companies').orderbychild("creation").startat(!null);
  • you add ".indexon": "creation" rules.

edit 2: curious, pushed 11,000 records /companies2 (half creation children, half without). able retrieve 5500 matching records in ~4 seconds using above queries (or 1 of variants i've shown below).

edit 3: if you're running these queries frequently, might worth separate children of /companies 2 bins based presence of creation. way, can read 2 segments separately without having rely on queries.

factory

here revised factory (i've revised plnkr match):

app.factory("companiesfactory",function($q, fburl){   return function(hascreation){     var deferred = $q.defer();     var ref = new firebase(fburl+'/companies').orderbychild("creation");     var query;     if (hascreation) {       query = ref.startat(!null);       // or:        // query = ref.startat(true);     } else {       query = ref.equalto(null);       // or:       // query = ref.endat(!null);       // query = ref.endat(true);     }     query.once("value", function(datasnapshot){       deferred.resolve(datasnapshot.val());     }, function (error) {       deferred.reject(error);     });     return deferred.promise;   } }); 

and yes, possible call .on('child_added') on returned datasnapshot. see datasnapshot.ref().



original answer using parameter:

(keeping reference)

another way adding parameter called hascreation children of companies have creation, , query that.

data

  • the query var ref = new firebase(fburl+'/companies').orderbychild("hascreation").equalto(hascreation);
    • if hascreation in query null, query return companies without hascreation child.
    • if hascreation in query true, query return companies hascreation===true.
{   "company1" : {     "creation" : {       "name" : "company1"     },     "hascreation" : true   },   "company2" : {     "name" : "company2"   },   "company3" : {     "name" : "company3"   },   "company4" : {     "creation" : {       "name" : "company4"     },     "hascreation" : true   } } 

rules

you add ".indexon" : "hascreation" rules so:

  "so:29179389":{     ".read" : true,     ".write" : true,     "companies" : {       ".indexon" : "hascreation"     }   } 

companies factory

app.factory("companiesfactory",function($q, fburl){   return function(hascreation){     var deferred = $q.defer();     if (!hascreation) {       hascreation = null;     }     var ref = new firebase(fburl+'/companies').orderbychild("hascreation").equalto(hascreation);     ref.once("value", function(datasnapshot){       deferred.resolve(datasnapshot.val());     });     return deferred.promise;   } }); 

controller

app.controller('homecontroller',function($scope,fburl,companiesfactory) {  $scope.getcompanies = function(hascreation) {   var companies = new companiesfactory(hascreation).then(function(data){      console.log(data);      $scope.companies = data;    });  } }); 

html

<body ng-app="sampleapp">   <div ng-controller="homecontroller">     <button ng-click="getcompanies(true)">find creation</button>     <button ng-click="getcompanies(false)">find without creation</button>     <h2>companies:</h2>     {{companies}}   </div> </body> 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -