Firebase Realtime Database and HTTP requests: complex queries over multiple axes












1














I have the following code to retrieve data with timestamps within the given dates and hour ranges:



app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
return snapshot.ref.orderByKey().startAt(`${hourFrom}`).endAt(`${hourTo}`).once('value').then( (s) => {
console.log(s.val());
return res.status(200).send(s.val());
});
})
});


The database in question looks like follows:
enter image description here



That is, for easy retrieval, I first bucket the items by date, then within the date -- by hour. So, 12 in the above image is 12:00PM.
Now, Firebase does not allow chaining two orderByKey()s, that is why I am trying to query first by date, and then, given a snapshot, trying to iterate over the children using snapshot.forEach(child), like so:



    app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
acc= ;
snapshot.forEach( (day) => {
day.forEach((hour) => {
console.log("This: " + hour.key);
if (hourFrom <= hour.key && hour.key <= hourTo)
hour.forEach((meal) => {
acc.push(meal.val());
});
});
});
return res.status(200).send(acc);
})
});


My question is, it is still possible to somehow chain the queries along the two axes, or an approach similar to the second code snippet is the only way to go?










share|improve this question
























  • There are two possible meanings here. Do you want meals from dayFrom at hourFrom until dayTo at hourTo? Or do you want meals on for each day from dayFrom until dayTo, between hourFrom and hourTo?
    – Frank van Puffelen
    Nov 19 '18 at 17:56










  • @FrankvanPuffelen: The second code snippet gets the job done, and indeed it is the second meaning -- the so-called "orthogonal range reporting", in CS parlance.
    – Ilonpilaaja
    Nov 19 '18 at 18:01






  • 1




    That looks like it's the second option from my first comment. There is no way to query two properties in a single query, and from a quick scan it seems like you also can't combine these with a single synthetic property. It looks like you'll need a query for each day at least, unless there are limits that you didn't share (e.g. the time ranges are a fixed, limited number). Also see stackoverflow.com/questions/26700924/…
    – Frank van Puffelen
    Nov 19 '18 at 18:04










  • Now I see I indeed used the approach "filter first at server, and the rest at client" approach in my Android code, first binding to a query and additionally filtering in onChildAdded method. As for this question, I am using the second snippet, then.
    – Ilonpilaaja
    Nov 19 '18 at 18:08
















1














I have the following code to retrieve data with timestamps within the given dates and hour ranges:



app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
return snapshot.ref.orderByKey().startAt(`${hourFrom}`).endAt(`${hourTo}`).once('value').then( (s) => {
console.log(s.val());
return res.status(200).send(s.val());
});
})
});


The database in question looks like follows:
enter image description here



That is, for easy retrieval, I first bucket the items by date, then within the date -- by hour. So, 12 in the above image is 12:00PM.
Now, Firebase does not allow chaining two orderByKey()s, that is why I am trying to query first by date, and then, given a snapshot, trying to iterate over the children using snapshot.forEach(child), like so:



    app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
acc= ;
snapshot.forEach( (day) => {
day.forEach((hour) => {
console.log("This: " + hour.key);
if (hourFrom <= hour.key && hour.key <= hourTo)
hour.forEach((meal) => {
acc.push(meal.val());
});
});
});
return res.status(200).send(acc);
})
});


My question is, it is still possible to somehow chain the queries along the two axes, or an approach similar to the second code snippet is the only way to go?










share|improve this question
























  • There are two possible meanings here. Do you want meals from dayFrom at hourFrom until dayTo at hourTo? Or do you want meals on for each day from dayFrom until dayTo, between hourFrom and hourTo?
    – Frank van Puffelen
    Nov 19 '18 at 17:56










  • @FrankvanPuffelen: The second code snippet gets the job done, and indeed it is the second meaning -- the so-called "orthogonal range reporting", in CS parlance.
    – Ilonpilaaja
    Nov 19 '18 at 18:01






  • 1




    That looks like it's the second option from my first comment. There is no way to query two properties in a single query, and from a quick scan it seems like you also can't combine these with a single synthetic property. It looks like you'll need a query for each day at least, unless there are limits that you didn't share (e.g. the time ranges are a fixed, limited number). Also see stackoverflow.com/questions/26700924/…
    – Frank van Puffelen
    Nov 19 '18 at 18:04










  • Now I see I indeed used the approach "filter first at server, and the rest at client" approach in my Android code, first binding to a query and additionally filtering in onChildAdded method. As for this question, I am using the second snippet, then.
    – Ilonpilaaja
    Nov 19 '18 at 18:08














1












1








1







I have the following code to retrieve data with timestamps within the given dates and hour ranges:



app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
return snapshot.ref.orderByKey().startAt(`${hourFrom}`).endAt(`${hourTo}`).once('value').then( (s) => {
console.log(s.val());
return res.status(200).send(s.val());
});
})
});


The database in question looks like follows:
enter image description here



That is, for easy retrieval, I first bucket the items by date, then within the date -- by hour. So, 12 in the above image is 12:00PM.
Now, Firebase does not allow chaining two orderByKey()s, that is why I am trying to query first by date, and then, given a snapshot, trying to iterate over the children using snapshot.forEach(child), like so:



    app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
acc= ;
snapshot.forEach( (day) => {
day.forEach((hour) => {
console.log("This: " + hour.key);
if (hourFrom <= hour.key && hour.key <= hourTo)
hour.forEach((meal) => {
acc.push(meal.val());
});
});
});
return res.status(200).send(acc);
})
});


My question is, it is still possible to somehow chain the queries along the two axes, or an approach similar to the second code snippet is the only way to go?










share|improve this question















I have the following code to retrieve data with timestamps within the given dates and hour ranges:



app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
return snapshot.ref.orderByKey().startAt(`${hourFrom}`).endAt(`${hourTo}`).once('value').then( (s) => {
console.log(s.val());
return res.status(200).send(s.val());
});
})
});


The database in question looks like follows:
enter image description here



That is, for easy retrieval, I first bucket the items by date, then within the date -- by hour. So, 12 in the above image is 12:00PM.
Now, Firebase does not allow chaining two orderByKey()s, that is why I am trying to query first by date, and then, given a snapshot, trying to iterate over the children using snapshot.forEach(child), like so:



    app.get('/mymeals/filtered', async(req,res) => {
let dayFrom= req.query.dayFrom;
let dayTo= req.query.dayTo;
let hourFrom= req.query.hourFrom;
let hourTo= req.query.hourTo;
console.log(`${dayFrom}-${dayTo} ${hourFrom} ${hourTo}`);
let userId= req.user.uid;
return await dbroot.ref(`duplicates/${userId}`)
.orderByKey().startAt(`${dayFrom}`).endAt(`${dayTo}`).once('value').then( (snapshot) => {
console.log(snapshot.val());
acc= ;
snapshot.forEach( (day) => {
day.forEach((hour) => {
console.log("This: " + hour.key);
if (hourFrom <= hour.key && hour.key <= hourTo)
hour.forEach((meal) => {
acc.push(meal.val());
});
});
});
return res.status(200).send(acc);
})
});


My question is, it is still possible to somehow chain the queries along the two axes, or an approach similar to the second code snippet is the only way to go?







javascript firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 18:04

























asked Nov 19 '18 at 17:21









Ilonpilaaja

317214




317214












  • There are two possible meanings here. Do you want meals from dayFrom at hourFrom until dayTo at hourTo? Or do you want meals on for each day from dayFrom until dayTo, between hourFrom and hourTo?
    – Frank van Puffelen
    Nov 19 '18 at 17:56










  • @FrankvanPuffelen: The second code snippet gets the job done, and indeed it is the second meaning -- the so-called "orthogonal range reporting", in CS parlance.
    – Ilonpilaaja
    Nov 19 '18 at 18:01






  • 1




    That looks like it's the second option from my first comment. There is no way to query two properties in a single query, and from a quick scan it seems like you also can't combine these with a single synthetic property. It looks like you'll need a query for each day at least, unless there are limits that you didn't share (e.g. the time ranges are a fixed, limited number). Also see stackoverflow.com/questions/26700924/…
    – Frank van Puffelen
    Nov 19 '18 at 18:04










  • Now I see I indeed used the approach "filter first at server, and the rest at client" approach in my Android code, first binding to a query and additionally filtering in onChildAdded method. As for this question, I am using the second snippet, then.
    – Ilonpilaaja
    Nov 19 '18 at 18:08


















  • There are two possible meanings here. Do you want meals from dayFrom at hourFrom until dayTo at hourTo? Or do you want meals on for each day from dayFrom until dayTo, between hourFrom and hourTo?
    – Frank van Puffelen
    Nov 19 '18 at 17:56










  • @FrankvanPuffelen: The second code snippet gets the job done, and indeed it is the second meaning -- the so-called "orthogonal range reporting", in CS parlance.
    – Ilonpilaaja
    Nov 19 '18 at 18:01






  • 1




    That looks like it's the second option from my first comment. There is no way to query two properties in a single query, and from a quick scan it seems like you also can't combine these with a single synthetic property. It looks like you'll need a query for each day at least, unless there are limits that you didn't share (e.g. the time ranges are a fixed, limited number). Also see stackoverflow.com/questions/26700924/…
    – Frank van Puffelen
    Nov 19 '18 at 18:04










  • Now I see I indeed used the approach "filter first at server, and the rest at client" approach in my Android code, first binding to a query and additionally filtering in onChildAdded method. As for this question, I am using the second snippet, then.
    – Ilonpilaaja
    Nov 19 '18 at 18:08
















There are two possible meanings here. Do you want meals from dayFrom at hourFrom until dayTo at hourTo? Or do you want meals on for each day from dayFrom until dayTo, between hourFrom and hourTo?
– Frank van Puffelen
Nov 19 '18 at 17:56




There are two possible meanings here. Do you want meals from dayFrom at hourFrom until dayTo at hourTo? Or do you want meals on for each day from dayFrom until dayTo, between hourFrom and hourTo?
– Frank van Puffelen
Nov 19 '18 at 17:56












@FrankvanPuffelen: The second code snippet gets the job done, and indeed it is the second meaning -- the so-called "orthogonal range reporting", in CS parlance.
– Ilonpilaaja
Nov 19 '18 at 18:01




@FrankvanPuffelen: The second code snippet gets the job done, and indeed it is the second meaning -- the so-called "orthogonal range reporting", in CS parlance.
– Ilonpilaaja
Nov 19 '18 at 18:01




1




1




That looks like it's the second option from my first comment. There is no way to query two properties in a single query, and from a quick scan it seems like you also can't combine these with a single synthetic property. It looks like you'll need a query for each day at least, unless there are limits that you didn't share (e.g. the time ranges are a fixed, limited number). Also see stackoverflow.com/questions/26700924/…
– Frank van Puffelen
Nov 19 '18 at 18:04




That looks like it's the second option from my first comment. There is no way to query two properties in a single query, and from a quick scan it seems like you also can't combine these with a single synthetic property. It looks like you'll need a query for each day at least, unless there are limits that you didn't share (e.g. the time ranges are a fixed, limited number). Also see stackoverflow.com/questions/26700924/…
– Frank van Puffelen
Nov 19 '18 at 18:04












Now I see I indeed used the approach "filter first at server, and the rest at client" approach in my Android code, first binding to a query and additionally filtering in onChildAdded method. As for this question, I am using the second snippet, then.
– Ilonpilaaja
Nov 19 '18 at 18:08




Now I see I indeed used the approach "filter first at server, and the rest at client" approach in my Android code, first binding to a query and additionally filtering in onChildAdded method. As for this question, I am using the second snippet, then.
– Ilonpilaaja
Nov 19 '18 at 18:08












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379721%2ffirebase-realtime-database-and-http-requests-complex-queries-over-multiple-axes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379721%2ffirebase-realtime-database-and-http-requests-complex-queries-over-multiple-axes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

ts Property 'filter' does not exist on type '{}'

Notepad++ export/extract a list of installed plugins