Change ObjectID string for frontend

Hello,

I was wondering if there was a way to change the appearance of an ObjectID to display on the frontend. For example, an ObjectID looks like: 5f01a0cb4e35982a29087286. Ugly appearance on the front end, would love to have it display as a random fixed number, such as 23499.

Is this possible? I’m using nodejs/mongoose.

Thanks in advance :slight_smile:

Why are you displaying the ObjectId field in the front end user interface? What is the purpose?

It’s for comments. Made the ObjectId a link to click which you could reply to. Should I not do this?

You can simply use a sub-string of the ObjectId string, for example showing the last five characters : [ ...87286] or just [...]. Does the application user identify the document using the visible ObjectId value (I think not).

Hi Matthew,

Just to add one more idea to what @Prasad_Saya already said, I think by “ObjectId” you mean the content of the _id field.

Note that ObjectId is just an auto-generated value if you don’t provide an _id field in the document you wanted to insert into MongoDB. If you provide your own _id field, it will be used instead, provided that it’s unique to the collection. So if you want the _id to be meaningful, you can supply your own values.

Best regards,
Kevin

1 Like