1

My Firebase Array has the following structure:

[
   {
  'userId': '12345',
  'itemOrdered' : 'abc',
  'status': 'pending'
   ...other attributes
   },
  {
  'userId': '6789',
  'itemOrdered' : 'def',
  'status' : 'pending',
   ...other attributes
   },
  {
  'userId': '12345',
  'itemOrdered' : 'def',
  'status' : 'complete',
   ...other attributes
   },

]  

I am not able to figure out how to retrieve the following data:

  1. Get records with userId = xxx
  2. Get all records where 'itemOrdered" = 'def'

Firebase docs talk about using orderByChild but that doesn't make much sense.

1

2 Answers 2

2

Assuming you're using the JavaScript SDK to access Firebase:

  1. ref.orderByChild('userId').equalTo('xxx')

  2. ref.orderByChild('itemOrdered').equalTo('def')

If you're trying to build a query that gets order of item def from user xxx, then that's not currently possible with Firebase's querying. The only way to query the value of multiple properties is to combine them in a single property in a way that allows the query you want. E.g.

  1. ref.orderByChild('userId_itemOrdered').equalTo('xxx_def')
Sign up to request clarification or add additional context in comments.

2 Comments

Frank..that's exactly what I am trying to achieve.. get all items that user xxx ordered. when you say that's not possible in firebase... I dont' quiet understand your statement... what exactly does your solution .orderBY().equalTo() do then on the server side ?
Sorry, I misread your sample data. In my defense, items abc and def are somewhat hard to interpret. What I mean is that you can only query on a single property. So you can get all items ordered, but not all def that user xxx ordered (unless you combine those property values, like I show in #3). I'll update my answer.
0

There are additional options depending on your platform.

1) Query for the userId and then filter in code for the item you are looking for. 

2) Query for the userId and build another query based on those results.

3) Flatten your data!

For example: create a node called user_purchases and each child could be a userId node with that users purchased itemId's (that makes it a snap to know exactly what items a user purchased). Or create an items_purchased node with each child being an item number node and then associated userId's who purchased that item.

Comments

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.