[push] Firebase: how to generate a unique numeric ID for key?

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.

This question is related to push firebase unique identity

The answer is


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)


https://firebase.google.com/docs/firestore/manage-data/transactions

Use transactions and keep a number in the database somewhere that you can increase by one. This way you can get a nice numeric and simple id.


Adding to the @htafoya answer. The code snippet will be

const getTimeEpoch = () => {
    return new Date().getTime().toString();                             
}

As the docs say, this can be achieved just by using set instead if push.

As the docs say, it is not recommended (due to possible overwrite by other user at the "same" time).

But in some cases it's helpful to have control over the feed's content including keys.

As an example of webapp in js, 193 being your id generated elsewhere, simply:

 firebase.initializeApp(firebaseConfig);
  var data={
      "name":"Prague"
  };
  firebase.database().ref().child('areas').child("193").set(data);

This will overwrite any area labeled 193 or create one if it's not existing yet.


Examples related to push

Best way to "push" into C# array Firebase: how to generate a unique numeric ID for key? Why does Git tell me "No such remote 'origin'" when I try to push to origin? Unknown SSL protocol error in connection Git push rejected "non-fast-forward" How to add multiple files to Git at the same time git push to specific branch Declare an empty two-dimensional array in Javascript? What does '--set-upstream' do? fatal: 'origin' does not appear to be a git repository

Examples related to firebase

How can I solve the error 'TS2532: Object is possibly 'undefined'? Getting all documents from one collection in Firestore FirebaseInstanceIdService is deprecated Failed to resolve: com.google.firebase:firebase-core:16.0.1 NullInjectorError: No provider for AngularFirestore Firestore Getting documents id from collection How to update an "array of objects" with Firestore? firestore: PERMISSION_DENIED: Missing or insufficient permissions Cloud Firestore collection count iOS Swift - Get the Current Local Time and Date Timestamp

Examples related to unique

Count unique values with pandas per groups Find the unique values in a column and then sort them How can I check if the array of objects have duplicate property values? Firebase: how to generate a unique numeric ID for key? pandas unique values multiple columns Select unique values with 'select' function in 'dplyr' library Generate 'n' unique random numbers within a range SQL - select distinct only on one column Can I use VARCHAR as the PRIMARY KEY? Count unique values in a column in Excel

Examples related to identity

Firebase: how to generate a unique numeric ID for key? ASP.NET MVC 5 - Identity. How to get current ApplicationUser SQL Server 2012 column identity increment jumping from 6 to 1000+ on 7th entry Auto increment primary key in SQL Server Management Studio 2012 What is the difference between == and equals() in Java? SQL Server, How to set auto increment after creating a table without data loss? Where is the Microsoft.IdentityModel dll What is the difference between Scope_Identity(), Identity(), @@Identity, and Ident_Current()? Why does comparing strings using either '==' or 'is' sometimes produce a different result? Handling identity columns in an "Insert Into TABLE Values()" statement?