Unfortunately, I can’t figure out how to turn the _id ObjectID, into a _id with a unique number generated by a number.
For example:
Right now the generation is like this
[
{
_id: 'adioj2ouro21jr9o3',
// ...
}
]
And we need to
[
{
id: 1,
// ...
}
]
2
Answers
https://www.mongodb.com/basics/mongodb-auto-increment
I have not used mongoDB myself. It appears that mongoDB doesnt support auto increment like MySQL would.
Could you use javascript to add N+1 on the last id in the table and manually create the ID field as needed?
The build-in mechanism in mongoDB to auto generate ObjectId() is a very good and easy way to have unique _id and also contain the insertion date which sometimes make troubleshooting easier.
You cannot replace having default _id key with id , but you can have both _id and id …
However you can insert different document in _id instead of the default ObjectId().
if you want the _id to be number you can read max(_id) and insert new document with inc(max(_id)) but this is not scalalble solution since if your writes increase it can become a bottleneck at some point.
Finally it is recomended to leave the default ObjectId() as your auto generated _id …