3

I have a firebase database who contains all purchase of my users

purchase
    userId1
         purchaseId1
         purchaseId2
         purchaseId3
   userId2
         purchaseId1
         purchaseId2
         purchaseId3

A purchase object (ex: purchaseId1) contains a price and a specific code

{
 price:100,
 code:'2XBE'
}

I search a query to find all purchase with the code '3XB1'

I use react-redux-firebase

Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lionel B
  • 1,172
  • 2
  • 9
  • 24

1 Answers1

1

you need to change the structure of your database

purchase : {
      purchaseId : {
           price:100,
           code:'2XBE'
           userId : 1
      }     
}

now you can do this

firebase.database().ref().child("purchase")
                   .orderByChild("code").equalsTo("3XB1")
                   .on( snap => console.log(snap.val() )
Ali Faris
  • 17,754
  • 10
  • 45
  • 70