I try to retrieve all the grand parent objects when a grandchild key is matched. In the Firebase realtime database below, I have for example the sensor key "-LNVBiM1SGoAFvlMfi1V", and I need to retrieve with it all the orders objects where this key is found (04TBR7 in this example).
I know the database structure might not be perfect but there is no possibility to modify it for now.
I tried the following code but it is not right, I guess the equalTo is not working since it is an Object:
db.ref('/orders').orderByChild('sensors').equalTo(sensor_id).once('value', snapshot => {
// my snapshot is always empty ...
});
There is no way to use orderByChild("sensors") in the way you're trying, as it checks the value of the sensors property (not the key).
It's important to keep in mind that Firebase database queries work on a flat list of nodes. So you can search for a specific order property across all orders, and you can search sensors under a specific order. But you can not search across all sensors for all orders.
Since you say you can't modify the data structure, that unfortunately means you'll have to load the entire JSON and find the matching nodes client-side.
Also see:
Firebase query if child of child contains a value
Firebase Query Double Nested
Related
I'm a Firebase newbie and I would like to figure out how to access deep nested data in Realtime Database.
Let's suppose I have the following structure:
I would like to get the block of data in which location -> Id value corresponds to 3154380, (in this case is the second one)
How could achieve this ?
Just put the sub-path in .orderByChild and use .equalTo:
database().ref('reports').orderByChild('location/id').equalTo('3174380');
However, I will say, you should really look at flattening your data structure for best performance and usability in this environment.
I want to read data from firebase, in the ref ("locations/{{someLocation}}/logs/{{someDevice}}")
but I do not know if exists a correct form to do, because I need to use ".on" to hear constantly all the devices changes, the problem is that I need to read all devices in logs in all locations, but I do not want another information, I just need information in logs or in the specific device, I know that I could reach that invoking one callback ".on" for each device, but I want a cleaner form to make it and in the documentation, there is no help for this.
maybe I could invoke a method like that
firebase.database("locations/{eachLocation}/logs/{eachDevice}").ref().on...
because I do not want all the devices, the problem is that I do not know if there is a form to make it thank you.
It sounds like you've nested your data too much. The Firebase documentation has explicit sections on avoiding nesting data and flattening data structures with hints on how to prevent this.
At first glance you'll need at least two top-level lists: locations and locationLogs. Under each you have the same keys as you have now, but the logs are now under /locationLogs/$key instead of under /locations/$key/logs. With that change you can get the logs for a location without getting the other data for that location.
If you don't know the location key, but don't want to get the logs for all locations, it sounds like you've nested another level too deep. Firebase queries work on a flat list of nodes, and can't search across multiple levels of unknown keys. If you want to search across all logs across all locations, you will need to keep a flat list of all logs. You can then tie each log back to its location, by adding the location ID to each log.
So that might lead to a structure of:
logs: {
"adjustableLight....1": {
locationId: "DTZB35",
date: 156...,"
...
}
}
What you're trying to do isn't supported by Realtime Database. There are no wildcard queries or placeholders. You must be able to build the full path to the node whose data you want to get.
Consider changing the structure of your data so that you can more easily find the nodes you want. It is common in nosql type databases to duplicate data into structures that are easier to query for a particular use case.
I want to store the comma separated ids on a child node & how can I filter data as in sql we can use IN clause to fetch data any possibility in firebase to perform this kind of operation in firebase database.
Please suggest any possible solution for this.
Firebase Realtime Database doesn't have the equivalent of SQLs IN clause. It also doesn't have a way to find a substring in a value. So the data model you are looking to use, doesn't allow the use-case you want. As usual with NoSQL databases, the solution is to pick a data model that does allow your use-case..
The most likely cause I know for the structure you describe is to associate the child node with a bunch of categories. If that is your case, read my answer here for a proper data structure: Firebase query if child of child contains a value
This is one of the cases where the new Cloud Firestore database offers better querying support, since it recently added a feature to efficiently test if an array contains a certain value (video). If you're only just getting started with your project, you might want to check if Firestore is a better fit for your use-cases.
I want to write data into a specific location in the database. Let's say, I have a couple of users in the database. Each of them has their own personal information, including their e-mails. I want to find the user based on the e-mail, that's to say by using his e-mail (but I don't know exactly whose e-mail it is, but whoever it is do something with that user's information). To be more visible, here is my database sample.
Now, while working on one of my javascript files, when the user let's say name1 changes his name, I update my object in javascript and want to replace the whole object under ID "-LEp2F2fSDUt94SRU0cx". To cut short, I want to write this updated object in the path ("Users/-LEp2F2fSDUt94SRU0cx") without doing it by hand and just "knowing" the e-mail. So the logic is "Go find the user with the e-mail "name1#yahoo.com" and replace the whole object with his new updated object". I tried to use orderByChild("Email").equalTo("name1#yahoo.com").set(updated_object), but this syntax does not work I guess. Hopefully I could explain myself.
The first part is the query, that is separate from the post to update. This part is the query to get the value:
ref.child('users').orderByChild("Email").equalTo("name1#yahoo.com")
To update, you need to do something like this once you have the user id from the query result:
ref.child('users').child(userId).child("Email").update(newValue);
firebase.database.Query
A Query sorts and filters the data at a Database location so only a
subset of the child data is included. This can be used to order a
collection of data by some attribute (for example, height of
dinosaurs) as well as to restrict a large list of items (for example,
chat messages) down to a number suitable for synchronizing to the
client. Queries are created by chaining together one or more of the
filter methods defined here.
// Find all dinosaurs whose height is exactly 25 meters.
var ref = firebase.database().ref("dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
console.log(snapshot.key);
});
plnkr
I am trying to traverse through a collection, and update each document respectively.
My UserProfile collection consists of multiple JSON objects of userProfiles. As you can see, each profile has a lot of the same information. The only difference is the personal information. (This is just a test case of hard coded objects. The real data will be in an SQL DB managed by a sysadmin).
What I am trying to do is write a function (replaceTopics) that will take in an array of topics and replace each topic that matches in the collection. So if the system admin makes a change to a topic/s, he will send me the topic/s and I will be checking each document in my userProfile collection to see if that document has the matching topic (by matching topicIDs), if so, I need to replace that entire topic with the editedTopic.
I have tried this but with no luck. You can take a look at my function.