19

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jcuenod
  • 55,835
  • 14
  • 65
  • 102

2 Answers2

31

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:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 8
    Are there any plans to change this? It makes it very difficult to write efficient value listeners if you're always forced to download all the descendants of a node. – chetbox Feb 17 '17 at 16:21
  • 4
    I would also like to request to add shallow option to the iOS SDK. I have over 1.5million items in my database. – FlatDog Apr 21 '17 at 23:35
  • 1
    @Frank van Puffelen, thanks for the response. Are there any plans to add the shallow query to the Firebase SDK? Also, is there any reason it wasn't already added in the past? – Rbar Aug 24 '17 at 00:57
  • Shallow in sdk seems counterproductive to Firebase philosophy.They want apps to behave as if operating from local database..shallow will just kill the experience if overused. In docs, they always ask us to flatten the data structure... – amar Apr 10 '18 at 06:00
3

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

Dr-Bracket
  • 4,299
  • 3
  • 20
  • 28