I need numeric IDs for human readability. How do I get it in Firebase?
I want numeric ID for keys, e.g. "000000001", "000000002","00000003","00000004".
The reason I need it is because these IDs will become the permanent object ID both online and offline. I want users to be able to browse that object page by just entering URL "/objects/00000001" without efforts.
I am asking here, because I want to know if this can be done without using .priority
, sub-properties, etc. I guess set
method can do it somehow. If it is not possible, just tell me no, I can accept that answer.
If you're not familiar with Firebase's push IDs, they are the chronological, 20-character unique IDs generated when you write lists of data to Firebase from any of our client libraries.
If you want a single unique ID, you should only call UUID. randomUUID(). toString() once and store the value in something like SharedPreferences between app starts. Note that this is pretty similar to the purpose of Firebase Instance IDs, so you might also consider using that to identify the device.
The keys generated by calling add() in Firestore are not tied to the collection on which you call add() . Instead they are random identifiers that are statistically guaranteed to be unique.
I'd suggest reading through the Firebase documentation. Specifically, see the Saving Data portion of the Firebase JavaScript Web Guide.
From the guide:
Getting the Unique ID Generated by push()
Calling
push()
will return a reference to the new data path, which you can use to get the value of its ID or set data to it. The following code will result in the same data as the above example, but now we'll have access to the unique push ID that was generated
// Generate a reference to a new location and add some data using push() var newPostRef = postsRef.push({ author: "gracehop", title: "Announcing COBOL, a New Programming Language" }); // Get the unique ID generated by push() by accessing its key var postID = newPostRef.key;
Source: https://firebase.google.com/docs/database/admin/save-data#section-ways-to-save
key
. These keys look like -JiGh_31GA20JabpZBfa
, so not numeric.From the guide:
In JavaScript, the pattern of calling
push()
and then immediately callingset()
is so common that we let you combine them by just passing the data to be set directly topush()
as follows. Both of the following write operations will result in the same data being saved to Firebase:
// These two methods are equivalent: postsRef.push().set({ author: "gracehop", title: "Announcing COBOL, a New Programming Language" }); postsRef.push({ author: "gracehop", title: "Announcing COBOL, a New Programming Language" });
Source: https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push
As explained above, you can use the Firebase default push id.
If you want something numeric you can do something based on the timestamp to avoid collisions
f.e. something based on date,hour,second,ms, and some random int at the end
01612061353136799031
Which translates to:
016-12-06 13:53:13:679 9031
It all depends on the precision you need (social security numbers do the same with some random characters at the end of the date). Like how many transactions will be expected during the day, hour or second. You may want to lower precision to favor ease of typing.
You can also do a transaction that increments the number id, and on success you will have a unique consecutive number for that user. These can be done on the client or server side.
(https://firebase.google.com/docs/database/android/read-and-write)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With