Home node.js How do I use _id in MongoDB?

How do I use _id in MongoDB?

Author

Date

Category

can’t figure out how to use this identifier in any way? In SQL it was somehow clearer, there are just numbers, but here ..


Answer 1, authority 100%

MongoDB does not support transactionality , so using numeric IDs ( namely, auto-increment primary keys) is highly undesirable. In theory, this can be done, for example like this , but it makes little sense.

ObjectId is a 12 byte hex string that consists of UNIX Timestamp, machine id, process id, and a random counter. This gives such an identifier uniqueness and the ability to sort by it. So, in fact, it can do everything the same as a numeric id in MySQL, only instead of an integer you will have a hexadecimal.

From the experience of working with projects that came to me with numerical ID specialists in Mong, I can say with confidence that even with a slight increase in the load, support for such an architecture turns into hell. Duplicate records constantly appear, records in the database are mixed with each other, auto-increment jumps, and it is almost impossible to work with this.

If you have specific questions or problems when working with such id – ask more specifically.

See also: BSON Types


Answer 2

I just added another identifier that I use as in sql, I read somewhere that the standard id can be configured. I don’t remember exactly how, it’s like using mongoose if the project is on nodejs

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions