2

CABARAN is an unknown Uid. it is not a text. Right now I have the uid, and I want to get value for the tajukPenuh.

Figure 1: Database

This is the code and I still can't get the value.

FirebaseDatabase.getInstance().getReference().child("karangan").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {

                Karangan karangan = child.child(karanganID).getValue(Karangan.class);

                if (karangan != null) {
                    String tajukPenuh = karangan.getTajukPenuh();

                    holder.getTextViewKaranganID().setText("Karangan Tajuk: " + tajukPenuh);

                }
            }

        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • 1
    If both "CABARAN" and "-LYgFIl4Xiv_Ls51Slvh" are unknown values, there is no way to query the database for a node under that. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Jun 23 '19 at 14:34
  • Please make sure that your code can run as is. What is the value of `karanganID`? If you use a hard-coded value for `karanganID`, can you still reproduce the problem? If so, update your question to show the reproduction with that hard-coded value. Also: please replace the picture of the JSON tree with the actual JSON as text, which you can easily get by clicking the Export JSON link in the overflow menu (⠇) of [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, and allows us to more easily use it. – Frank van Puffelen Jun 23 '19 at 14:36
  • @FrankvanPuffelen "-LYgFIl4Xiv_Ls51Slvh" this value is not unknown value. Only "CABARAN" is an unknown value. – Ticherhaz FreePalestine Jun 23 '19 at 14:49

1 Answers1

2

You could in theory do a query like this:

FirebaseDatabase.getInstance().getReference().child("karangan")
  .orderByChild("-LYgFIl4Xiv_Ls51Slvh/uid").equalTo("-LYgFIl4Xiv_Ls51Slvh")
  .addListenerForSingleValueEvent(new ValueEventListener() {

But the problem is that you'd need a lot of indexes in your rules, which may be technically possible, but is unfeasible for most real usage.

Your current data structure makes it easy to find all the child nodes for CABARAN, but it does not make it easy to find CABARAN for a given child node. To allow that use-case to run efficiently, you should expand your data structure with a so-called reverse index that maps back to CABARAN from the value that you know. So something like:

"myIndex": {
  "-LYgFIl4Xiv_Ls51Slvh": "CABARAN",
  "-LzfFIl4Xasas51Slads": "CABARAN",
  "-Lasddas981398asdh1h": "CASITWO"
}

This is an additional data structure, that you will have to keep up to date when you're writing the rest of the data. But with this structure, it now becomes very easy to determine that -LYgFIl4Xiv_Ls51Slvh maps to CABARAN.

For more on this, see my answer here: Firebase query if child of child contains a value

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I was wondering about the usage data read, download and performance. I thought when we store the same value many times in the database, it is not good, isn't? – Ticherhaz FreePalestine Jun 23 '19 at 15:15
  • 1
    It's quite common to store the same data multiple times in NoSQL databases, as they tend to be used in cases where you optimize for fast reads. I'd recommend reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/), and watching [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s). – Frank van Puffelen Jun 23 '19 at 16:19