Why we cannot define primary key ourself in mongodb? -
the _id
field reserved primary key in mongodb
. why mongodb
designed this? , can define primary key myself?
yes, can define own primary key collection. note mongodb drivers automatically generate unique _id values; however, can override _id value:
eg. db.yourcollection.insert({_id:"myuniquevaluen",a:1,b:1})
you can create secondary indexes enforce uniqueness. refer below: http://docs.mongodb.org/manual/tutorial/create-a-unique-index/
i'm not sure if understand why part of question. mention _id primary key , serves point of ensuring uniqueness of document within collection, , means of retrieving documents unique id. it's purpose no different primary keys in other databases. why pre-defined? it's partly due fact mongodb assigns unique object id value automatically if don't specify 1 purpose. having standard _id field simplifies implementation.
Comments
Post a Comment