0

my receipe data in firebase ![my receipe data in firebase][1]

My receipe data looks like the following

receipe/1/12: "{"itemno":"1","receipeno":"12","receipedescript..."

How can I filter all receipes where receipeno = 12 or starting with receipeno = 12, irrespective of itemno?

I have tried the following, but got no results or errors? Also tried startAt

this.query = this.db.database.ref("receipe").orderByChild("receipeno").equalTo("12").limitToFirst(100);

BTW: this.query = this.db.database.ref("receipe") this returns all data and this.db.database.ref("receipe/1") returns all receipe for itemno == 1.

I have updated the data to not use opaque strings. [Updated db so as not to use opaque strings][2]

and have tried the following.

this.query = this.db.database.ref("receipe").orderByChild("itemno").equalTo("1").limitToFirst(100);

And

this.query = this.db.database.ref("receipe").orderByChild("receipeno").equalTo("r1").limitToFirst(100);
3
  • 1
    Did you try anything? Commented Jun 14, 2017 at 1:15
  • Well, the first thing that's stopping you is that you appear to be saving the data as opaque JSON strings. Commented Jun 14, 2017 at 1:22
  • It's "recipe", not "receipe". Commented Jun 14, 2017 at 2:38

1 Answer 1

1

Firebase is not going to help you with deep queries. It filters and sorts at one level. Therefore, there is no way to say, "find me all the recipes with an id (or recipeno) of 12, under any node under recipes.

If you had a single level, then you would do orderByKey().equalTo("12"). Of course this will limit you to one recipe per ID.

If you are going to have multiple recipes with the number 12 and want to do this kind of querying, you need to change your database structure--essentially getting rid of the intermediate level where you currently have keys of 1, 2 etc. Most likely in this case you would use automatically generated keys of the kind that result from push, such as:

+ recipes
  + autokey1: {itemno, recipeno, recipedescription}
  + autokey2: {itemno, recipeno, recipedescription}

Then you could say

orderByChild('recipeno').equalTo('12')

This assumes, of course, as pointed out in a comment, that you are saving the data as objects, not as stringified JSON, which Firebase will never be able to query against the insides of.

This is a good case study of the notion that you should design your Firebase database structure carefully in advance to allow you to do the kinds of querying you will need to do.

By the way, this question has nothing to do whatsoever with Angular.

Sign up to request clarification or add additional context in comments.

2 Comments

I think you have identified this as a limitation of the firebase platform. If the keys are autokeys, then I am not sure if I can enforce the uniqueness of itemno + receipeno combination without additional complex code. Also not sure if these nested objects are allowed to be written why I cannot query them? Firebase seems like a hierarchical database but it not.
we have come up with a new design by have a key like itemno + recipeno with | in between, thus minimizing nesting of objects. |item1|recipe1 |item2|recipe2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.