I want to set one of my fields as primary key. I am using MongoDB as my NoSQL.
_id field is reserved for primary key in mongodb, and that should be a unique value. If you don't set anything to _id it will automatically fill it with "MongoDB Id Object". But you can put any unique info into that field.
All documents in a MongoDB collection have a primary key dubbed _id . This field is automatically assigned to a document upon insert, so there's rarely a need to provide it. What's interesting about the _id field is that it is time based.
MongoDB ReferencesNormally, MongoDB doesn't work with the concept of foreign keys. The foreign keys can be used in the Relational Databases for visualizing data from multiple collections simultaneously.
Architecturally, by default the _id field is an ObjectID, one of MongoDB's BSON types. The ObjectID is the primary key for the stored document and is automatically generated when creating a new document in a collection.
_id
field is reserved for primary key in mongodb, and that should be a unique value. If you don't set anything to _id
it will automatically fill it with "MongoDB Id Object". But you can put any unique info into that field.
Additional info: http://www.mongodb.org/display/DOCS/BSON
Hope it helps.
The other way is to create Indexes
for your collection and make sure that they are unique.
You can find more on the following link
I actually find this pretty simple and easy to implement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With