1

I'm aware of the function .equalTo(0) in Firebase to find a result. However how can the same be achieved in a nested object? Let's say I have the following data in my Products database:

Object {
  "codes": Object {
    "0": "12314241",
    "1": "253525523523",
  },
  "name" : "hello",
  ...
}
Object {
  "codes": Object {
    "0": "4634347347473",
    "1": "3562552335",
  },
  "name" : "bye",
    ...
}

If I just wanted to find an entry which isn't nested I could execute something like

db.ref('/products').orderByChild("name").equalTo("hello").once('value', querySnapShot => { if (querySnapShot.exists()) { ... } })

Now I want to see if a code exists in any of my nested entries. How is this achieved? Is it possible to .equalTo() on nested objects, I'm stuck.

I tried the following:

const search = "4634347347473"
    await db
    .ref('/products')
    .once('value', querySnapShot => {
        if (querySnapShot.exists()) {
        querySnapShot.forEach((child) => { 
            let val = child.child("barcodes").val()
            console.log(child)
            
        })
    }
})

I can't figure out how to find if an entry which I'm searching for exists in my any of my nested objects.

Thanks in advance, looking forward to learn.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
DanteKlein
  • 31
  • 1
  • 4
  • The value to search for must be at a fixed location under each *immediate* child node of `products`. So while your data structure makes it easy to find the codes for a specific product, it doesn't allow searching for the products for a specific code. To allow that you'll want to introduce an additional data structure, typically referred to as a inverted index. For more on this see https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value and https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen May 23 '21 at 03:46

0 Answers0