Is it bad to change _id type in MongoDB to integer?

前端 未结 3 1329
感动是毒
感动是毒 2020-12-03 04:29

MongoDB uses ObjectId type for _id.

Will it be bad if I make _id an incrementing integer?

(With this gem, if you\'re interested)

3条回答
  •  春和景丽
    2020-12-03 05:19

    You can do it, but you are responsible to make sure that the integers are unique.

    MongoDB doesn't support auto-increment fields like most SQL databases. When you have a distributed or multithreaded application which has multiple processes and/or threads which create new database entries, you have to make sure that they use the same counter. Otherwise it could happen that two threads try to store a document with the same _id in the database.

    When that happens, one of them will fail. That means you have to wait for the database to return a success or error (by calling GetLastError or by setting the write concerns to acknowledged), which takes longer than just sending data in a fire-and-forget manner.

提交回复
热议问题