Retrieve cross objects child from Firebase Database

firebase get all child keys android
retrieve data from firebase android
firebase get data by key android
firebase get child value javascript
how to retrieve data from firebase in android studio
firebase get all child keys javascript
retrieve multiple data from firebase android
firebase get value of child

I am trying to get weight and quantity from Cat1 where item_name, date and branch are same in Cat2 in Firebase Realtime DB. But I don't know how to check conditions for that.

JSON

"Cat1" : {
    "-LBBWypuYLVn-aDYWK-N" : {
      "branch" : "b1",
      "date" : "28/Apr/2018",
      "weight" : "46",
      "item_name" : "item1",
      "quantity" : "20"
    },
    "-LBBZKUlEJB-HdiKftoj" : {
      "branch" : "b2",
      "date" : "28/Apr/2018",
      "weight" : "112",
      "item_name" : "item2",
      "quantity" : "16"
    }
  },
"Cat2" : {
    "-LBBWQcC3acYk-OOiX3T" : {
      "branch" : "b1",
      "date" : "28/Apr/2018",
      "item_name" : "item1",
      "weight" : "2.7",
      "quantity" : "20"
    }
  }

So far I have done the following:

mFishQuery = mFishRef.orderByChild("date").equalTo(mawStock.getDate());
//        mFishQuery = mFishRef.orderByKey();//.equalTo(mawStock.getDate());
//        Query fishWeight = mFishQuery.orderByChild("item_name").equalTo(mawStock.getItem_name()).getRef().orderByChild("quantity").equalTo(mawStock.getQuantity());
mFishQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        mWeightF = (Double) dataSnapshot.child("fish_weight").getValue();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

mFishRef.child("date").equalTo(mawStock.getDate()).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Try the following:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Cat2");
ref.orderByChild("item_name").equalTo("item1").addSingleValueEventListener(new ValueEventListener(){
  @Override
public void onDataChange(DataSnapshot dataSnapshot) {
 for(DataSnapshot datas: dataSnapshot.getChildren()){
    String date=datas.child("date").getValue().toString();

    DatabaseReference refer=FirebaseDatabase.getInstance().getReference().child("Cat1");
    refer.orderByChild("date").equalTo(date).addSingleValueEventListener(new ValueEventListener(){
         @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       //retrieve data
      }
        @Override
    public void onCancelled(FirebaseError firebaseError) {

       }
      });
    }

  @Override
public void onCancelled(FirebaseError firebaseError) {

 }
});

When you query, you can only use one child so one orderByChild() or orderByKey()

So, here you have the datasnapshot at child Cat2 then you query where item_name=item1 and you retrieve the date from the database.

Then you add a nested listener and do a query where date= date_retrieved, and retrieve the data of Cat1.

android, Anonymous Authentication · Enabling cross-app authentication with shared iOS Calling val() / getValue() on a snapshot returns the a language-specific object is passed a snapshot containing all data at that location, including child data. Because the SDK converts posts to objects by retrieving the value, you have  The ChildAdded event is typically used to retrieve a list of items in a Firebase database. The ChildAdded event is raised once for each existing child and then again every time a new child is added to the specified path. The listener is passed a snapshot containing the new child's data. The ChildChanged event is raised any time a child node is modified. This includes any modifications to descendants of the child node.

Basically, you just have to do enough itr.next() until the iterator is at the nth position (where n is the random number from nextInt()) and then you can simply get the Object you want with getValue(), the example below should show it well:

Solution to get random generated keys.

questionsRef = FirebaseDatabase.getInstance().getReference().child("questions").child("DE");

questionsRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int questionCount = (int) dataSnapshot.getChildrenCount();
        int rand = random.nextInt(questionCount);
        Iterator itr = dataSnapshot.getChildren().iterator();

        for(int i = 0; i < rand; i++) {
            itr.next();
        }
        childSnapshot = (DataSnapshot) itr.next();
        question = childSnapshot.getValue(Question.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Retrieving Data, Anonymous Authentication · Enabling cross-app authentication with shared iOS To read data from the database, you need an instance of DatabaseReference : ChildAdded, Retrieve lists of items or listen for additions to a list of items. Child events are triggered in response to specific operations that happen to the  I am new to Firebase and Android. I have stored user authentication details (name, profile image other than email ID) in my Firebase account. I want to retrieve those data (such as names etc.,) to the other part of my app. How can I retrieve my realtime database child values?

Finally I solved the problem with the help of @Peter Hadad's answer with some modification in his answer.

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Cat1");
ref.orderByChild("item_name").equalTo("item1").addSingleValueEventListener(new ValueEventListener(){
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot datas : dataSnapshot.getChildren()){

        DatabaseReference refer=FirebaseDatabase.getInstance().getReference().child("Cat1");
        refer.orderByChild("date").equalTo(date).addSingleValueEventListener(new ValueEventListener(){

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if (dataSnapshot.exists()) {

                    for (Datasnapshot ds : dataSnapshot) {

                        List<Datasnapshot> list = new ArrayList<>();
                        list.add(ds);

                        //compare data and apply logic

                    }

                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }
});

Retrieving Data, Why couldn't you just have a recipe object with a breakfast property that is an How can I get data from Firebase and retrieve it in an Angular template chart You can go like firebase.database().ref('/whatever-you-crossed-in-red').child('  When using orderBy with the name of a child key, data that contains the specified child key will be ordered as follows: Children with a null value for the specified child key come first. Children with a value of false for the specified child key come next.

How to access multiple child nodes in Firebase under the parent , We can retrieve List that contains all Data items by collecting each item in Child To get the best cross-browser support, it is a common practice to apply The “​book” is the child of the database. js Firebase Tutorial is today's primary focus. Retrieve Multiple Data - Android Firebase Tutorial - Full Backend Course (Part - 13) Retrieve data on ListView from Firebase Realtime Database Part 4 - Duration:

Firebase Get All Child Keys Javascript, Cross-Platform: It can be used for building a back-end for various platforms like Android, Firebase Realtime Database stores data as one large JSON tree. If data is stored in well formatted structure then it is easy to save as well as retrieve it easily. Insert the user-defined object to the database reference.child("user"). In the next section on Retrieving Data, we'll learn how to read this data from a Firebase database. Saving Transactional Data. When working with complex data that could be corrupted by concurrent modifications, such as incremental counters, the SDK provides a transaction operation.

Firebase RealTime Database with Operations in Android with , Firebase provides very little guidance on how to structure your unstructured JSON data. Firebase provides two primary event types for retrieving your data, value and child_added. The value event returns all child nodes in an unsorted JSON object and then How to upload data to Firebase Firestore Cloud Database. The Firebase Realtime Database can be accessed directly from a mobile device or web browser; there’s no need for an application server. Security and data validation are available through the Firebase Realtime Database Security Rules, expression-based rules that are executed when data is read or written.

Comments
  • yeah, i m working on it.
  • Thank you for this answer, buddy. It helped me get some logic to move further. But still I am unable to fulfill the condition.
  • Which condition do you mean?
  • Like cat1.item_name=cat2.item_name and cat1.date=cat2.date and cat1.quantity=cat2.quantity.
  • yes that is mostly because you can only use one orderByChild(), you may need to change the database a bit. But in the above, it checks if item_name is equal to item1 in cat1 and retrieves the other data example date. Then it checks if cat1 has the same date(as cat2) , so it retrieves the data of cat1
  • Cool. Now i got it. I have to add 1 more listener to fulfill this.