Firebase Query Double Nested -


given data structure below in firebase, want run query retrieve blog 'efg'. don't know user id @ point.

{users :      "1234567": {           name: 'bob',           blogs: {                'abc':{..},                'zyx':{..}           }      },      "7654321": {           name: 'frank',           blogs: {                'efg':{..},                'hij':{..}           }      } } 

the firebase api allows filter children 1 level deep (or with known path) orderbychild , equalto methods.

so without modifying/expanding current data structure leaves option retrieve data , filter client-side:

var ref = firebase.database().ref('users'); ref.once('value', function(snapshot) {     snapshot.foreach(function(usersnapshot) {         var blogs = usersnapshot.val().blogs;         var dablog = blogs['efg'];     }); }); 

this of course highly inefficient , won't scale when have non-trivial number of users/blogs.

so common solution so-called index tree maps key looking path resides:

{blogs:      "abc": "1234567",      "zyx": "1234567",      "efg": "7654321",      "hij": "7654321" } 

then can access blog using:

var ref = firebase.database().ref(); ref.child('blogs/efg').once('value', function(snapshot) {     var user = snapshot.val();     ref.child('blogs/'+user+'/blogs').on('value, function(blogsnapshot) {         var dablog = blogsnapshot.val();     }); }); 

you might want reconsider if can restructure data better fit use-case , firebase's limitations. have documentation on structuring data, important 1 people new nosql/hierarchical databases seems "avoid building nests".


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -