M
M
m4tkv2019-12-11 14:40:26
Node.js
m4tkv, 2019-12-11 14:40:26

How to get the id of a child in firebase without knowing the parent?

I am using firebase realtime database on node.js .
The data structure is as follows:

{
...
batch-1:
        id-1(Условно):
                     name:...
                     phone:...
        id-2:
             ...
        id-3:
             ...
batch-2:
        ...
batch-3:
        ...
...


batch-n:
       ...
}

How can I get the element by id-1 without knowing which batch it is in. Identifiers of type id-1 are unique and must be short. If batch was known, then I would go like this
let ref = admin.database().ref('batches/batch-1/id-1');
    ref.on('value', (snapshot) => res.json(Object.assign({}, snapshot.val())));

or even so
let ref = admin.database().ref('batches/');
    ref.child().orderByKey().equalTo('1').on('value', (snapshot) => res.json(Object.assign({}, snapshot.val())));

But how can I get an element by ID (id-1) without knowing the batch number (batch)? Or will such a data structure not be able to produce a good result?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question