Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
Firebase retrieve a document based on grandchild's value which is a collection

Is there a way to Query a collection which is a grandchild in firebase?

Current firebase structure:

{
  products: {
    "dfwojozijowjfoije": {
      "barcodes": ["12345", "5678"],
      "productName": "someProduct"
    },
    "sdafsdasdfasdfadsf": {
      "barcodes": ["99999", "88888"],
      "productName": "someProduct2"
    }
  }
}

Current Query that I use:

await firebase
            .database()
            .ref('products')
            .orderByChild('barcodes')
            .equalTo('88888')
            .on('value', snapshot => {
                    setProductName(snapshot.val())
                }
            )
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There is no way in a Firebase Realtime Database query to check for the existence of a value in an array. If you're trying to do this, it typically means that your data structure is actually not an array, but a set.

Since a set data type doesn't exist, you can't store it in Firebase natively though. The closest equivalent is not an array though, but a map like this:

"barcodes": {
  "12345": true,
  "5678": true
}

This may look a bit weird at first, but it has the exact properties that you're typically looking for in a set: the values (that are now keys) are by definition unique in the parent node, and you can test for the presence of a specific value/key.

Unfortunately, you still won't be able to query on this structure, as you can only define indexes on keys that you know, and I'm assuming that the barcodes are a rather infinite set of values.

So instead you'll have to define an inverted data structure, as I've explained here: Firebase query if child of child contains a value

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!