Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the key from the value in firebase

How do I get the key "-KLpcURDV68BcbAvlPFy" when I know the field "name" contains "efg" in the following structure in Firebase.

clubs
    -KLpcURDV68BcbAvlPFy
        dept: "abc"
        desc: "xyz"
        name: "efg"
    -asdasdasddsad
        dept: "asda"
        desc: "asd"
        name: "adddd"

I tried this but it returned "clubs"

mDatabase.child("clubs").orderByChild("name").equalTo("efg").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String clubkey =dataSnapshot.getKey();
like image 295
THe_strOX Avatar asked Jul 06 '16 19:07

THe_strOX


People also ask

How do I get keys from Firebase?

Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

Is Firebase key value?

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

How do I find my unique key in Firebase?

You can create a unique key in Firebase database using push() and then add child nodes under that key. Now next time when you come to that activity, first check if parent node exists or not. If the node exists, save parent node key and use it to save new child nodes.

What is key in Firebase database?

The key elements of Firebase database list handling are the push() method, the Query class, child and value event listeners and a range of filtering options.

How to get the key from Firebase?

To get the key from firebase you can try following in your code: itemsRef.observe (.value, with: { snapshot in for child in snapshot.children { if let alldata = child as?

What are “child key values” in Firebase?

What are the “Child Key Values”. The “Child Key Values” are defined as that type of the values, where the various types of the sub-values are been calculated respectively with the help of the tokens which are used in the respective “Firebase Database” respectively. III.

How do I restrict access to Firebase API?

Be sure your Firebase project is still selected. Click Create credentials > API key. Take note of the new API key, then click Restrict key. In the API restrictions section, select Restrict key, then add to the list only the Super Service API. This new API key grants access only to the Super Service API.

How do I Secure my Cloud storage data with Firebase?

Secure your database and Cloud Storage data by using Firebase Security Rules, not by restricting and/or obscuring your API keys. A Firebase project can have many API keys, but each API key can only be associated with a single Firebase project. Firebase automatically creates API keys for your project when you do any of the following:


3 Answers

If anyone needs to do this using Kotlin:

mDatabase.child("clubs")
        .orderByChild("name")
        .equalTo("efg")
        .addListenerForSingleValueEvent(object: ValueEventListener {

            override fun onDataChange(dataSnapshot: DataSnapshot) {

                dataSnapshot.children.forEach {
                     //"it" is the snapshot
                     val key: String = it.key.toString()
                }
            }

            override fun onCancelled(p0: DatabaseError) {
                    //do whatever you need
            }
         })
like image 153
Rob Avatar answered Oct 27 '22 20:10

Rob


That's because you're using a ValueEventListener. If the query matches multiple children, it returns a list of all those children. Even if there's only a single matches child, it's still a list of one. And since you're calling getKey() on that list, you get the key of the location where you ran the query.

To get the key of the matches children, loop over the children of the snapshot:

mDatabase.child("clubs")
         .orderByChild("name")
         .equalTo("efg")
         .addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            String clubkey = childSnapshot.getKey();

But note that if you assume that the club name is unique, you might as well store the clubs under their name and access the correct one without a query:

mDatabase.child("clubs")
         .child("efg")
         .addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String clubkey = dataSnapshot.getKey(); // will be efg
like image 23
Frank van Puffelen Avatar answered Oct 27 '22 18:10

Frank van Puffelen


firebaseDatabase = FirebaseDatabase.getInstance();
mReference = firebaseDatabase.getReference("clubs");

mReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot ds: dataSnapshot.getChildren()){
            String key = ds.getKey();
        }
    }
}
like image 3
Malik Ali Avatar answered Oct 27 '22 18:10

Malik Ali