How to set primary key in MongoDB?

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

+48
mongodb
Jul 21 2018-10-21T00:
source share
8 answers
Field

_id reserved for the primary key in mongodb, and this must be a unique value. If you do not set anything in _id , it will automatically populate it with the “MongoDB Id Object”. But you can put any unique information in this field.

Additional information: http://www.mongodb.org/display/DOCS/BSON

Hope this helps.

+66
Jul 21 '10 at 12:11
source share

Another way is to create Indexes for your collection and make sure they are unique.

Further information can be found at the following link.

I actually find it pretty simple and easy to implement.

+12
Dec 18 '15 at 7:36
source share

In the mongodb field, _id is reserved for the primary key. Mongodb uses the internal value of ObjectId if you do not define it in your object, and also create an index to ensure performance.

But you can put your own unique value for _id, and Mongodb will use it instead of making it for you. And even if you want to use several fields as a primary key, you can use an object:

 { _id : { a : 1, b: 1} } 

Just be careful when creating these identifiers that have the order of keys (a and b in the example), if you exchange them, this is considered another object.

+6
Jan 22 '16 at 15:48
source share

You can simply use db.collectionName.createIndex({urfield:1},{unique:true});

+4
Jul 20 '16 at 7:10
source share

If you think that RDBMS cannot create a primary key, the default primary key is _id. But you can create a unique pointer . An example is below.

 db.members.createIndex( { "user_id": 1 }, { unique: true } ) db.members.insert({'user_id':1,'name':'nanhe'}) db.members.insert({'name':'kumar'}) db.members.find(); 

The output is below.

{"_id": ObjectId ("577f9cecd71d71fa1fb6f43a"), "user_id": 1, "name": "nanhe"}

{"_id": ObjectId ("577f9d02d71d71fa1fb6f43b"), "name": "kumar"}

When trying to insert the same error writing icon user_id mongodb.

 db.members.insert({'user_id':1,'name':'aarush'}) 

WriteResult ({"nInserted": 0, "writeError": {"code": 11000, "errmsg": "Collection of errors with duplicate keys E11000: student.members index: user_id_1 dup key: {: 1.0}"}})

+2
Jul 12 '16 at 5:58
source share

This is the syntax for creating a primary key

dB & l .; collection> .createIndex (<key and index type specification>, {unique: true})

Suppose our database has a collection called student , and the document has a key called student_id , which we need to make a primary key. Then the team should look like this.

 db.student.createIndex({student_id:1},{unique:true}) 

You can check if this student_id is set as the primary key by adding a duplicate value to the student collection.

prefer this document for further information https://docs.mongodb.com/manual/core/index-unique/#create-a-unique-index

+2
Jun 16 '17 at 9:42 on
source share

http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

You can also check this link and automatically increase the _id field.

+1
Aug 20 '13 at 11:15
source share

One way to achieve this behavior is to set the value to _id (which is reserved for the primary key in MongoDB) based on custom fields that you want to treat as the primary key.
those. if I want employee_id as the primary key, then while creating the document in MongoDB; assign the value of _id in the same way as the value of employee_id .

+1
Aug 06 '15 at 13:51
source share



All Articles