2

I realise this question is marked as a duplicate - but I think this question is more comprehensive and easier to understand, and therefore more likely to help others. (^__^)

I have read about reading data from Firebase realtime database. https://firebase.google.com/docs/database/admin/retrieve-data

What I have not found is any documentation on how to control what data gets returned from the read query.

Consider the following structure:

-users
  -EiYu7OWooToo6noo
    alias: "Kermit"
    createdAt: 1538330532000
    firstName: "Kermit"
    surName: "Frogman" 
    pictureURL: "/imgs/kermit.png"
    phonePrimary: "555-1234"
    email: "kermit@themuppetshow.com"
    -myBooks
      -A1234567890
        title: "Frogs 101"
      ...

Is there a way to tell Firebase realtime database to only return some of the document data?

This code returns the entire document:

var ref = db.ref(`users/${uid}`);

ref.once("value", (snapshot) => {
  console.log(snapshot.val());
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});

Would it be possible to only get a partial document, for example:

    {
      alias: "Kermit"
      firstName: "Kermit"
    }

My reasoning is that this would be a huge performance gain when reading large amount of data. But is it possible?

Kermit
  • 2,865
  • 3
  • 30
  • 53

1 Answers1

3

You can't select a subset of child nodes for a query. Every query at a node always fetches the entire contents of the node - all the children and their children.

If you want only a subset of children, you have to perform different queries for each one. Alternatively, you can organize your data in such a way that commonly grouped fields are collected together at a particular node.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441