How to randomly generate objectid in node js

I need to randomly generate an objectid in node js.Is there is some way to create it.

+4
source share
2 answers

If you mean the object identifier MongoDB, try the following:

var ObjectID = require('mongodb').ObjectID; var objectId = new ObjectID(); 
+23
source

Let me know if this is what you are looking for.

 // On top of your script, or whatever var crypto = require('crypto'); // wherever you want function randomObjectId() { return crypto.createHash('md5').update(Math.random().toString()).digest('hex').substring(0, 24); } 

With any latest version of cryptography, you can use the abbreviation:

 crypto.randomBytes(12).toString('hex') 
+3
source

Source: https://habr.com/ru/post/1438608/


All Articles