'Check if some key value exists in Firebase Database and do some action Android

I want to check if a specific value of a key exists in the Realtime Database or not and perform some action based on it.

I have the following data:

"Orders" : {
"03403426747" : {
  "17" : {
    "State" : "(4) Canceled",
    "address" : "yubt",
    "date" : "Feb 28, 2022",
    "discount" : "0",
    "name" : "uk",
    "phone" : "0311111111",
    "time" : "15:33:58 PM",
    "totalAmount" : "3778"
  },
  "18" : {
    "State" : "(1) Approved",
    "address" : "yubt",
    "date" : "Feb 28, 2022",
    "discount" : "120",
    "name" : "uk",
    "phone" : "03111111111",
    "time" : "16:01:58 PM",
    "totalAmount" : "7703"
  }
 }
}

I want to check If any order from these has has "State" value other than "(3) Completed" & "(4) Canceled".

if anyone's order has a value other than these, I want to remove that user from the list which contains users with pending orders. and if at any time that user has new order or older order State changed I want to again add that user to the list.



Solution 1:[1]

I want to check If any order from these has has "State" value other than "(3) Completed" & "(4) Canceled".

There is no way you can query the Realtime Database using a negation. What you can do instead is to create a separate query for each "other" state and join the results on the client.

However, if you consider at some point in time to try using Cloud Firestore, then you should consider using not equal (!=) query or if it fits your needs the not-in query.

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 Alex Mamo