0

I would like to know how can I retrieve the key of this object based on the object content. I'm using a realtime firebase database.

JSON code :

"Places" : {
"-M11-OIIHbKTVqCJ-Swv" : {
  "name" : "Farmacia Pagani Dr. Roberto",
  "others" : {
    "Abilify" : 100,
    "Acetaminophen" : 120,
    "Acyclovir" : 140,
    "Adderall" : 160
  }
},
"-M11-OJJCGAPdJUknLXX" : {
  "name" : "FARMACIA DR. TUMIATTI MARIANO",
  "others" : {
    "Abilify" : 100,
    "Acetaminophen" : 120,
    "Acyclovir" : 140,
    "Adderall" : 160
  }
}

I want to retrieve M11-OIIHbKTVqCJ-Swv if I have the name : Farmacia Pagani Dr. Roberto"

The code I wrote :

mUserDatabase = FirebaseDatabase.getInstance().getReference("Places");

Query query = mUserDatabase.orderByChild("name").equalTo(nameOfThePharmacy);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
                                                 @Override
                                                 public void onDataChange(DataSnapshot dataSnapshot) {
                                                     System.out.println("The key is : "+dataSnapshot.getKey());

                                                 }

                                                 @Override
                                                 public void onCancelled(DatabaseError databaseError) {
                                                     throw databaseError.toException();
                                                 }
                                             });

This code doesn't work, it returns : "Places" but I want the key of the object that contains the field name that equals with = ad example Farmacia Pagani Dr. Roberto , the output should be : -M11-OIIHbKTVqCJ-Swv

Alex97
  • 401
  • 1
  • 8
  • 21
  • 3
    Everything you describe sounds like it's possible. But your question is way too broad to answer in a reasonably succinct post here on Stack Overflow. It's much more likely we can help if you break your problem into discrete steps, start working on the first one, and if you get stuck post back here with a [minimal, complete/standalone reproduction of that problem](http://stackoverflow.com/help/mcve). The smaller the problem you post, the more likely it is that someone can help. For example: finding nearby locations, see https://stackoverflow.com/questions/43357990/query-for-nearby-locations – Frank van Puffelen Feb 26 '20 at 17:46
  • I edited so it's easy to understand and I just added a part of my problem – Alex97 Feb 26 '20 at 18:05
  • 1
    That is indeed a much better scoped question, which makes it much easier to help. What you're looking for is known as a database **query**. Instead of writing a new answer for you, I recommend looking at this one I gave yesterday: https://stackoverflow.com/questions/60405198/firebase-get-specfic-chid-from-parent/60405520#60405520 If you're having a hard time mapping that to your use-case, update your question with the query you tried, and I'lll have another look. – Frank van Puffelen Feb 26 '20 at 18:12
  • I followed what you told me and I get the result, check the bellow the new query. Thank you in advance. – Alex97 Feb 26 '20 at 18:25
  • 1
    You misssed the `for (DataSnapshot studentSnapshot: dataSnapshot.getChildren()) {` from the answer I linked. Sicne a query can match multiple child nodes, you need to loop through the results. – Frank van Puffelen Feb 26 '20 at 18:44
  • Thank you, now it's working, I thought that I don't need the for loop since the name is unique. – Alex97 Feb 26 '20 at 18:50
  • 1
    When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. – Frank van Puffelen Feb 26 '20 at 18:51
  • Ah ok, now I understood how a query works, thank you so much. When I will be able to post another question I will ask it there since is the last question I'm going to ask for this app that I'm doing. I need just one more thing to understand how to do it and then I finished my project. Thank you again. You are incredible, – Alex97 Feb 26 '20 at 18:54

0 Answers0