Joins in the Firebase Database - The Firebase Database For SQL Developers #5
Why Stack Overflow recommends it
Stack Overflow users link it when someone asks how to do a SQL-style join or related-data lookup in Firebase, since it shows the multiple-listener pattern step by step.
Recommended by 6 people across 6 different months, 2016 to 2020.
What it is
An 8-minute episode of the official Firebase series 'The Firebase Database for SQL Developers'. It explains that the Realtime Database has no built-in join, and shows how to combine data from several paths by attaching multiple listeners to denormalized data.
What people said
Recommends André Luiz · 2016-12 · Stack Overflow · on “AngularFire: how to run join queries”
I followed the instruction in this video [this video] and I managed to solve the problem. It sounds weird to me to make subsequent calls but I guess there is not much troubles with it. The function: $scope.getMessagesByRegion = function(regionId){ var rootRef = firebase.database().ref(); var regionMessagesRef = rootRef.child("region_messages/"+ regionId); $scope.messages_by_region = []; // Here I reset the scope variable regionMessagesRef.on('child_added', function(rmSnap) { var messageRef = rootRef.child("messages/"+rmSnap.key); messageRef.once('value').then(function(msgSnap){ var msg = { key : msgSnap.key, name : msgSnap.val().name, type : $scope.getTypeName(msgSnap.val().type), show_only_once
Recommends Salmin Skenderovic · 2017-01 · Stack Overflow · on “How to query Firebase database object when criteria property is nested?”
I think there are several good examples on these two videos. I think the second video answers your question, but watch both since they are very easy to follow: #1: [LINK] and #2: [this video]
Recommends Peter · 2017-09 · Stack Overflow · on “Advice on the DB structure of a firebase Chat application”
It's impossible to say what the best approach is since we don't know the end game of your application is. Will you have a need to search for messages, users etc? Will you want to list users? Since you mention: "if a user changes his avatar only the new messages will display it" you have already answered you own question I think. You should normalize the data so messages and users are separate. Firebase isn't a relational DB so you wouldn't join any tables. You query the messages. Once you get the first message you can start to query users from the user id in the message. You'll end up querying once for messages and 1 query for each user you need. You can be smart about querying for users and
Recommends Attila Szász · 2018-03 · Stack Overflow · on “Firebase database Android - mapping datasnapshot child with different structure to java object”
You should refactor a bit your database. You should put those nodes, which value are true into a separate node (I call them here Ids ). Also for the list1, list2, ... nodes you should create a parent node. For exampe: "lists": { "list1": { "category": "food", ... }, "list2": { ... }, ... }, "ids": { "8ada6af0-...": true, ... } Now if you create another custom object(let it be Data) which contains: // the inner map is one list object Map<String, Map<String, ProductFirebase>> mProducts; Map<String, Ids> mIds; The Ids object should contain: Map<String, Boolean> mIds; Now, with this structure you can get the data, how you already did: Data data = dataSnapshot.getValue(Data.class); I hope that th
Recommends Jainam Jhaveri · 2018-05 · Stack Overflow · on “Is there a way in Firebase to automatically swap a list of id's into the object”
What I think from your description is that you want to perform a join. There's no official "join" method in the Firebase Database SDK, but you can use multiple listeners to combine data from multiple paths. [this video] check out this youtube video it will help
Recommends shahzeb akram · 2018-09 · Stack Overflow · on “Firebase database synchronous calls issue”
At last, I managed to find the solution on own my with the help of this link [this video] . I am sharing the helping link and my code maybe it will be helpful for some other person. $(".all_firebase_keys").each(function () { id = "#"+$(this).val(); commentsRef.child($(this).val()).orderByChild('timestamp').on("child_added", function (snap) { users = usersRef.child(snap.val().from_key); users.once("value", function (snapshot) { $("#"+snap.ref.parent.key).append('<p>'+snapshot.val().first_name +" "+ snap.val().comment+'</p>'); }) }); });
Cites Niklas · 2017-02 · Stack Overflow · on “Joins in the Firebase Database with Polymer Properties”
GOOD SOLUTION HERE [LINK] In the case below I am trying to get data from one location and then find the related data in a different location (the firebase joins) [this video] . I am able to retrieve the appropriate data and display in the my console but I kind of stuck when it comes to storing them in one of my properties to then loop over them with a dom-repeat template. In addition I am not entirely sure if this should be done with plane JavaScript or the PolymerFire components. //Key value for a course that I retrieved from a URL Query var ckey = KSH456YU789; //Get the Videos that belong to that course key firebase.database().ref('/courseVideos/' + ckey).on('child_added', snap => { //Get the v
Cites John · 2017-06 · Stack Overflow · on “Joins in the Firebase Database”
The firebase data structure is { "eventAttendees" : { "fm" : { "1" : "David", "2" : "Alice" } }, "events" : { "fm" : { "date" : "2017-06-16", "name" : "Firebase Meetup" }, "gm" : { "date" : "2017-08-12", "name" : "Meet Linh" } }, "users" : { "1" : { "email" : "david@gmail.com", "name" : "David" }, "2" : { "email" : "alice@gmail.com", "name" : "Alice" }, "10" : { "email" : "khanh@gmail.com", "name" : "Khanh" } } } I want to find all users who go to fm event. Here is my code: let ref = Database.database().reference() ref.child("eventAttendees/fm").observe(.value, with: { (snapshot) in print (snapshot.key) ref.child("users/\(snapshot.key)").observeSingleEvent(of: .value, with: { (userSnapshot)
Cites Supreeth Shanthakumar · 2017-09 · Stack Overflow · on “Firebase query join. Help needed”
Just started with Firebase and loving it. Was watching this video [this video] and wanted some help with a particular query. If anyone can help? In the schema/structure below if i wanted to query all the events that a person named "David" attended, what would it be? { "users": { "key1": { "name": "David", "age": 33, "profileImg": "http/......" } "key2": { "name": "John", "age": 25, "profileImg": "http/......" } }, "events": { "fm": { "name": "event1", "date": 12332443456 } }, "eventAttendees": { "fm": { "key1": "David", "key2": "John" } } } I'm kinda stuck and would like some help. Thanks. DB structure [LINK]
Cites Carlos · 2018-03 · Stack Overflow · on “How do I manipulate and print data from nested firebase lists in Ionic”
I'm working with Ionic and Firebase and I'm facing several problems with linked data, either to manipulate or print. I have read articles and watched several videos of different approaches between versions but couldn't solve it yet, and I have to recognize that right now I have a terrible mess (I am a beginner of course). Let me explain more details: Versions : "angularfire2": "^5.0.0-rc.6" "firebase": "^4.10.1" "ionic-angular": "3.9.2", Firebase structure sample : What I need to print : Right now I'm able to retrieve and print the order list using an and of course the field "user" is displayed as "-L5uzsn0C4-FYiS7HiY" but what I need to display is the name of the user "Carlos". Approaches :