How to not generate unique ID when using push method in Firebase? - firebase

I'm trying to push new data into Firebase but it keeps generating an unique ID and an extra child node for me. How can I stop generating the ID and extra node or is there another way to push data into Firbase? My dataset is given and I just need to dump the dataset into the db. (But I'm slowly updating this given dataset.)

Firease always generates the unique id when using the push method. You can use it as a key or as a parent node. This post describes push in more detail.
https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html
You should use set to publish new data to firebase if push doesn't fit your use case. There is also an update method as set will overwrite any data in the given path.
https://firebase.google.com/docs/database/web/save-data
I generally use set for new data, or if I change how I have my data structured, or if I am appending new data in a path, update to make changes to fields for a dataset, and push for sets of data that I want organized in a list by time( i.e. Chat message log ).

Related

How to retrieve child nodes data in firebase by php

How to retrieve child nodes data and update values in firebase:
As I want to make an API in which I create parameters to pass values by it.
Like example I have one node in firebase name as users and under it has user amount is 10. I want to get that data of amount from users node and add some value with it like 10+5 will update in database as 15 in my firebase.
I tried to retrieve data from firebase by seeing youtube, but I'm unable to get what I want; I was only able to get the whole users node data at whole.
That sounds like a 2-step process:
Store the value as a number rather than as a string like you now have it.
Then use the atomic increment operation to increment it by 5.

Get parent key Firebase Arduino

I'm trying to get the parent names of my Firebase database in Adruino. I want an array with (0123456789123, 0123456789000, etc). (see image for structure).
I only get the child names by doing:
FirebaseObject object = Firebase.get("/");
Serial.println(object.getString("0123456789123"));
or
Serial.println(Firebase.getStrung("0123456789123"));
How to get the database information if you don't exactly know the size or names of the parents?
Image of database structure:
As far as I can determine, you need to get the JSON object out of the Firebase data with Firebase.get("/").getJsonVariant().asObject(). Then you can use the methods of JsonObject to navigate the structure.
For a complete example, see
https://github.com/FirebaseExtended/firebase-arduino/issues/288#issuecomment-338631309

How to give the name to the child node in firebase

Whenever i will upload the data to the firebase it give random name to the child node. In the image "Upload" this name in given by me but what about inside name i want to change the name of inside the upload root how i can do that
The name inside the Upload is generated using push(), that you are using in your code, that is why you see it.
push() is a method that creates a random id to be able to seperate records in the database and also to be able to easily identify records in the Firebase Database.
push()
Create a reference to an auto-generated child location. The child key is generated client-side and incorporates an estimate of the server's time for sorting purposes. Locations generated on a single client will be sorted in the order that they are created, and will be sorted approximately in order across all clients.
more info here: https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseReference.html#push()

Get auto-Id by time

In my app I use Firebase's childByAutoId() (swift) or .push() (web) to insert some data in the following format:
- events
- $autoId
- time:
- name:
- $autoId
- time:
- name:
Where $autoId are the randomly generated keys Firebase makes. time is the epoch time of when the data was pushed.
I want to allow users to modify each inserted entry's time. However, I want to keep the nodes under events sorted by their key and by time which Firebase naturally does when you use .push(). But if they modify the time so that it should actually be in a different order, the entries won't be sorted correctly.
Is there a way to generate an id by the modified time so that if it were inserted into events it would be in the right order? That way I could just delete the old entry and insert the new one while just duplicating the data.
Since the algorithm for Firebase's push IDs is well documented, you could easily modify the function to generate them based on a specific timestamp.
But I'd recommend instead keeping the necessary values as named properties for each child node. If you need to be able to sort by both creation and modification time, keep two separate properties. That way you won't have to depend on the behavior of the push IDs, but instead use more explicitly named properties to accomplish what you need.

Does the path matter for push ids?

I've been creating a consumer who "grants" ids to clients when they perform certain tasks. It occurred to me at some point that it might be entirely superfluous to worry about the path before I do a .push().name() to create new ids.
Does it matter what path I run the .push().name() on to create a unique ID? Does Firebase generate the IDs entirely based on timestamp, without regard to the path the ID will be assigned to?
Currently, push() ids are generated based on timestamps (along with some randomness). The path on which the id is being pushed is not used as part of the id.

Resources