How to set a primary key in MongoDB?

前端 未结 9 989
再見小時候
再見小時候 2020-12-07 21:46

I want to set one of my fields as primary key. I am using MongoDB as my NoSQL.

9条回答
  •  半阙折子戏
    2020-12-07 22:16

    One way of achieving this behaviour is by setting the value to _id (which is reserved for a primary key in MongoDB) field based on the custom fields you want to treat as primary key.
    i.e. If I want employee_id as the primary key then at the time of creating document in MongoDB; assign _id value same as that of employee_id.

提交回复
热议问题