'Convert UUIDv4 to ObjectID - MongoDB

At the moment I am generating unique UUID's and save them as a string in MongoDB. Is there a way to store these UUID's as ObjectID in MongoDB.

The problem is that User.findOne based on _id doesn't work because UUID is a string and not a ObjectID



Solution 1:[1]

you don't have to generate the uuids of entities mongoose do it for you, for example if you use the create() method without passing the _id it will generate it.

it's sure better to hand-generate it.

and its by default.

Solution 2:[2]

So basically store them as _id: UUID("5fe6075a-8157-4dfc-8aab-3316ea9cbf17") format instead of string. Then it should work as expected with findOne(). Also you may refer to this answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco Bertelli
Solution 2 fractal397