Firebase realtime database read operation - Only get part of document returned possible? [duplicate]












1
















This question already has an answer here:




  • How to pull the data partially from firebase database

    1 answer




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?










share|improve this question















marked as duplicate by Frank van Puffelen firebase
Users with the  firebase badge can single-handedly close firebase questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • console.log(snapshot.val().alias + " " + snapshot.val().firstName);

    – Christophe Gudlake
    Nov 12 '18 at 18:06











  • Yes, that would filter the result but the entire document would still be returned from the query? I edited my question to be less ambiguous. Or did you mean I can specify what parts of the document I want returned from the query?

    – Kermit
    Nov 13 '18 at 8:09






  • 1





    If you target this var ref = db.ref(users/${uid}/alias); you will get only the alias

    – Christophe Gudlake
    Nov 13 '18 at 13:43






  • 1





    True, thanks! That would mean two requests, right? 1. ref = db.ref(users/${uid}/alias); 2. ref = db.ref(users/${uid}/firstName);

    – Kermit
    Nov 13 '18 at 16:49











  • yes but i'm not sure you will gain in performance with that.

    – Christophe Gudlake
    Nov 13 '18 at 16:58
















1
















This question already has an answer here:




  • How to pull the data partially from firebase database

    1 answer




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?










share|improve this question















marked as duplicate by Frank van Puffelen firebase
Users with the  firebase badge can single-handedly close firebase questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • console.log(snapshot.val().alias + " " + snapshot.val().firstName);

    – Christophe Gudlake
    Nov 12 '18 at 18:06











  • Yes, that would filter the result but the entire document would still be returned from the query? I edited my question to be less ambiguous. Or did you mean I can specify what parts of the document I want returned from the query?

    – Kermit
    Nov 13 '18 at 8:09






  • 1





    If you target this var ref = db.ref(users/${uid}/alias); you will get only the alias

    – Christophe Gudlake
    Nov 13 '18 at 13:43






  • 1





    True, thanks! That would mean two requests, right? 1. ref = db.ref(users/${uid}/alias); 2. ref = db.ref(users/${uid}/firstName);

    – Kermit
    Nov 13 '18 at 16:49











  • yes but i'm not sure you will gain in performance with that.

    – Christophe Gudlake
    Nov 13 '18 at 16:58














1












1








1









This question already has an answer here:




  • How to pull the data partially from firebase database

    1 answer




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?










share|improve this question

















This question already has an answer here:




  • How to pull the data partially from firebase database

    1 answer




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?





This question already has an answer here:




  • How to pull the data partially from firebase database

    1 answer








javascript firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 19:54







Kermit

















asked Nov 12 '18 at 18:04









KermitKermit

92114




92114




marked as duplicate by Frank van Puffelen firebase
Users with the  firebase badge can single-handedly close firebase questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Frank van Puffelen firebase
Users with the  firebase badge can single-handedly close firebase questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 19:40


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • console.log(snapshot.val().alias + " " + snapshot.val().firstName);

    – Christophe Gudlake
    Nov 12 '18 at 18:06











  • Yes, that would filter the result but the entire document would still be returned from the query? I edited my question to be less ambiguous. Or did you mean I can specify what parts of the document I want returned from the query?

    – Kermit
    Nov 13 '18 at 8:09






  • 1





    If you target this var ref = db.ref(users/${uid}/alias); you will get only the alias

    – Christophe Gudlake
    Nov 13 '18 at 13:43






  • 1





    True, thanks! That would mean two requests, right? 1. ref = db.ref(users/${uid}/alias); 2. ref = db.ref(users/${uid}/firstName);

    – Kermit
    Nov 13 '18 at 16:49











  • yes but i'm not sure you will gain in performance with that.

    – Christophe Gudlake
    Nov 13 '18 at 16:58



















  • console.log(snapshot.val().alias + " " + snapshot.val().firstName);

    – Christophe Gudlake
    Nov 12 '18 at 18:06











  • Yes, that would filter the result but the entire document would still be returned from the query? I edited my question to be less ambiguous. Or did you mean I can specify what parts of the document I want returned from the query?

    – Kermit
    Nov 13 '18 at 8:09






  • 1





    If you target this var ref = db.ref(users/${uid}/alias); you will get only the alias

    – Christophe Gudlake
    Nov 13 '18 at 13:43






  • 1





    True, thanks! That would mean two requests, right? 1. ref = db.ref(users/${uid}/alias); 2. ref = db.ref(users/${uid}/firstName);

    – Kermit
    Nov 13 '18 at 16:49











  • yes but i'm not sure you will gain in performance with that.

    – Christophe Gudlake
    Nov 13 '18 at 16:58

















console.log(snapshot.val().alias + " " + snapshot.val().firstName);

– Christophe Gudlake
Nov 12 '18 at 18:06





console.log(snapshot.val().alias + " " + snapshot.val().firstName);

– Christophe Gudlake
Nov 12 '18 at 18:06













Yes, that would filter the result but the entire document would still be returned from the query? I edited my question to be less ambiguous. Or did you mean I can specify what parts of the document I want returned from the query?

– Kermit
Nov 13 '18 at 8:09





Yes, that would filter the result but the entire document would still be returned from the query? I edited my question to be less ambiguous. Or did you mean I can specify what parts of the document I want returned from the query?

– Kermit
Nov 13 '18 at 8:09




1




1





If you target this var ref = db.ref(users/${uid}/alias); you will get only the alias

– Christophe Gudlake
Nov 13 '18 at 13:43





If you target this var ref = db.ref(users/${uid}/alias); you will get only the alias

– Christophe Gudlake
Nov 13 '18 at 13:43




1




1





True, thanks! That would mean two requests, right? 1. ref = db.ref(users/${uid}/alias); 2. ref = db.ref(users/${uid}/firstName);

– Kermit
Nov 13 '18 at 16:49





True, thanks! That would mean two requests, right? 1. ref = db.ref(users/${uid}/alias); 2. ref = db.ref(users/${uid}/firstName);

– Kermit
Nov 13 '18 at 16:49













yes but i'm not sure you will gain in performance with that.

– Christophe Gudlake
Nov 13 '18 at 16:58





yes but i'm not sure you will gain in performance with that.

– Christophe Gudlake
Nov 13 '18 at 16:58












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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.






    share|improve this answer




























      2














      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.






      share|improve this answer


























        2












        2








        2







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 18:26









        Doug StevensonDoug Stevenson

        72.4k983103




        72.4k983103















            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation