Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase (2016) Shallow Query

I'm trying out Firebase (since Google's new release).

In the original version of Firebase the parameter shallow=true would return an object with { key: true } for every key at the root of the tree/branch that was requested (and so, rather than children being returned, you would just know the fact that child(ren) exist). This is useful because you don't necessarily want all the data from the child nodes (particularly if there's a lot of it).

Is there a way to do that with Google's new version of Firebase? I'm thinking something like:

firebase.database().ref('/data/?shallow=true').once('value', function(snapshot) {
  // do something with snapshot
}

The above code's snapshot.val() returns null and if I'm reading the docs correctly, it seems this functionality is gone.

like image 988
jcuenod Avatar asked May 21 '16 17:05

jcuenod


People also ask

What is getKey () in Firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

Can we use SQL queries in Firebase?

FireSQL is a library built on top of the official Firebase SDK that allows you to query Cloud Firestore using SQL syntax. It's smart enough to issue the minimum amount of queries necessary to the Firestore servers in order to get the data that you request.

How do you filter data in Firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .


2 Answers

The ?shallow=true parameter in Firebase Database 2.x was only available in the REST API. See https://www.firebase.com/docs/rest/guide/retrieving-data.html#section-rest-uri-params.

In the new Firebase Database 3.x, the same parameter is still only available in the REST API. See https://firebase.google.com/docs/database/rest/retrieve-data#shallow

You're using a Firebase SDK (JavaScript from the looks of it), which never supported this parameter.

For more questions that have discussed this in the past, see:

  • Firebase retrieve child keys but not values
  • Firebase REST API working with shallow data
  • How do I do a shallow query on Firebase iOS?
  • Get Firebase child nodes' names without getting their children too in Firebase response?
like image 149
Frank van Puffelen Avatar answered Oct 02 '22 21:10

Frank van Puffelen


This worked for me based on Frank's answer:

import request from 'request';

request({ url: "https://[YOUR-APP-ID].firebaseio.com/path/to/data/.json?shallow=true" }, (error, response, body) => {
    const shallowData = JSON.parse(body);
    console.log(shallowData);
});

Reference: https://firebase.google.com/docs/database/rest/retrieve-data#shallow

like image 44
Dr-Bracket Avatar answered Oct 02 '22 22:10

Dr-Bracket