Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform join query in Firebase? [duplicate]

After retrieving group key in HashMap how to perform join query which shows only those grp details which have member as that particular user. And if this structure is wrong please help me with this.

Structure:

screenShot

like image 358
Arjun Thakkar Avatar asked Dec 14 '16 05:12

Arjun Thakkar


People also ask

What does getKey () do Firebase?

Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value. Using Firebase to generate unique keys in this way is also of particular use when an app has multiple users creating child nodes at the same path in the tree.

What is simultaneous connections in Firebase?

A simultaneous connection is equivalent to one mobile device, browser tab, or server app connected to the database. This isn't the same as the total number of users of your app, because your users don't all connect at once.

What is DataSnapshot in Firebase?

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot. DataSnapshots are passed to the methods in listeners that you attach with addValueEventListener , addChildEventListener , or addListenerForSingleValueEvent .


1 Answers

Use DatabaseReference inside another DatabaseReference:

 // any way you managed to go the node that has the 'grp_key'
    DatabaseReference MembersRef = FirebaseDatabase.getInstance()
            .getReference()
            .child("Members")
            .child("1CkPG20Tt2dzrVkYkdfCLo")
            .orderByKey().equalTo("-KYnhiAucnasdkadNC")
            .addValueEventListener(
                    new ValueEventListener()
                    {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot)
                        {
                            for (DataSnapshot child : dataSnapshot.getChildren())
                            {

                                Map<String, Object> valuesMap = (HashMap<String, Object>) dataSnapshot.getValue();

                                // Get push id value.
                                String key = valuesMap.get("grp_key");


                                // HERE WHAT CORRESPONDS TO JOIN
                                DatabaseReference chatGroupRef = FirebaseDatabase.getInstance().getReference()
                                        .child("Chat_groups")
                                        .orderByKey().equalTo(key)
                                        .addValueEventListener(
                                                new ValueEventListener()
                                                {
                                                    @Override
                                                    public void onDataChange(DataSnapshot dataSnapshot)
                                                    {
                                                        // repeat!!
                                                    }

                                                    @Override
                                                    public void onCancelled(DatabaseError databaseError)
                                                    {

                                                    }
                                                }
                                        )
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError)
                        {

                        }
                    }
            );
like image 119
amrro Avatar answered Oct 31 '22 21:10

amrro