'Firebase Query in Android returns random number of items altough multiple items match the query

I am using the Firebase Realtime database and I constructed a query for the node "Orders" which should return all items whole attribute "orderID" is equal to 2. In my database I have currently 3 items that match this query, meaning that they have the "orderID" 2. However, my query returns a random number of matching items (I checked that by using LogTag). Sometimes it returns nothing, sometimes it returns just the first matching item and sometimes it returns all items. The very strange thing is this randomness. I always execute the same code. I don't understand why it is not returning all of them as I use the loop for (DataSnapshot ds: dataSnapshot.getChildren()) to iterate over all items that should match the query. Any ideas on why this is happening and how I can tackle this problem?

DatabaseReference rootRef_Firebase = FirebaseDatabase.getInstance(FIREBASE_URL).getReference();
rootRef_Firebase
    .child("Orders")
    .orderByChild("oderID")
    .equalTo(2)
    .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

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

                String itemName="";
                if (ds.child("name").getValue(String.class)!=null) {
                    itemName= ds.child("name").getValue(String.class);
                }
                
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });

Update: Here is a part of the Firebase Realtime Database:

  "Orders": {
    "table_1_order_7_date_29-04-22_time_18-17-49": {
      "comment_Text": "",
      "name": "Order_A",
      "orderDate": "18:17 (29.04.22)",
      "orderDateInMilliseconds": 1651249069304,
      "orderID": 2,
      "orderOtherInfo": "",
      "orderStatus": "ordered",
      "quantity": 1,
      "tableNumber": 1
    },
    "table_1_order_8_date_29-04-22_time_18-17-52": {
      "comment_Text": "",
      "name": "Order_B",
      "orderDate": "18:17 (29.04.22)",
      "orderDateInMilliseconds": 1651249072115,
      "orderID": 2,
      "orderOtherInfo": "",
      "orderStatus": "prepared",
      "quantity": 1,
      "tableNumber": 1
    },
    "table_1_order_9_date_29-04-22_time_18-17-54": {
      "comment_Text": "",
      "name": "Order_C",
      "orderDate": "18:17 (29.04.22)",
      "orderDateInMilliseconds": 1651249074747,
      "orderID": 9,
      "orderOtherInfo": "",
      "orderStatus": "prepared",
      "quantity": 1,
      "tableNumber": 1
    }
  },


Solution 1:[1]

I cannot see any reasons why you get that randomness using the actual code. Maybe there is something else in your code, that we cannot see, that provides such a behavior. However, I'll try to provide the same solution but using get() and I highly recommend you try this in isolation, meaning without any other code in your activity class:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference ordersRef = db.child("Orders");
Query queryOrdesById = ordersRef.orderByChild("orderID").equalTo(2);
queryOrdesById.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot orderSnapshot : task.getResult().getChildren()) {
                String name = orderSnapshot.child("name").getValue(String.class);
                Log.d("TAG", name);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The number of results returned should always be and the 2, if you have at least two orders, and the result in the logcat will be:

Order_A
Order_B

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1