How to set primary key in mongodb

1,436 views
Skip to first unread message

Sankaravelu S

unread,
Jun 18, 2012, 7:18:53 AM6/18/12
to mongod...@googlegroups.com
Hi,

I want to set primary key for sno.

db.demo.insert({ "sno" : 1 , "username" : "hendry" })

Can any one please give the solution for this.


Thanks in advance.
Sankaravelu.S

Scott Hernandez

unread,
Jun 18, 2012, 7:23:04 AM6/18/12
to mongod...@googlegroups.com
By default the _id field has a unique index and can be considered the
primary key in the way you are probably thinking about it.

You can do this:
db.demo.insert({ "_id" : 1 , "username" : "hendry" })

If you leave out the _id field then one will be generated for you as
an ObjectId: http://www.mongodb.org/display/DOCS/Object+IDs
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
Reply all
Reply to author
Forward
0 new messages