0
    ref.child(eid).addValueEventListener(object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

            if(snapshot.exists()){
                val user = snapshot.getValue(UserModel::class.java)
            }
        }
    })

Currently, the code that I use above retrieves all the keys of the eid child. How can I retrieve only specific keys from the eid child?

The eid child has too much data. I don't need all keys.

Thanks

gal
  • 929
  • 4
  • 21
  • 39

1 Answers1

1

The only way is to filter data, you can use limitToFirst() which would return a limited number of nodes from the beginning of the list, or even you can use limitToLast() that will retrieve limited number of nodes from the end of the list.

For example:

ref.child(eid).limitToFirst(10).addValueEventListener(object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

            if(snapshot.exists()){
                val user = snapshot.getValue(UserModel::class.java)
            }
        }
    })

Or another way if you want to retrieve a specific value, then you can just use equalTo():

ref.child(eid).orderByChild("name").equalTo("Peter").addValueEventListener(object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

            if(snapshot.exists()){
                val user = snapshot.getValue(UserModel::class.java)
            }
        }
    })

The above query would work, if you have the following database:

eid
 random_id
   name: Peter

Check the following documentation for more information:

https://firebase.google.com/docs/database/android/lists-of-data#filtering_data

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134