Re: Primary key

85 views
Skip to first unread message

rktoomey

unread,
Jun 5, 2012, 9:07:09 AM6/5/12
to scala-salat
Hi Zoro,

The primary key in any Mongo collection is the "_id" field. The type
of the _id field is often an ObjectId but could be any type you
choose. See http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-The%5CidField
for more information.

case class Foo(_id: ObjectId)

Since some people prefer "id" to "_id", Salat has an @Key annotation
that can be used to change the name of the field when the object is
serialized (https://github.com/novus/salat/wiki/Annotations).

import com.novus.salat.annotations._

    case class Foo(@Key("_id") id: ObjectId)

If you want to index a business key, see http://www.mongodb.org/display/DOCS/Indexes
for information about how to create indices on collections. If you
have questions about that, please post to mongodb-casbah-users.

Best,
Rose

On Jun 5, 7:50 am, Zoro <khandra...@gmail.com> wrote:
> Is it possible to mark a field as primary key?

rktoomey

unread,
Jun 5, 2012, 12:15:22 PM6/5/12
to scala-salat
Right - you define unique indices on the Mongo collection itself, not
the model object.

See http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue
for more information.


On Jun 5, 10:32 am, Zoro <khandra...@gmail.com> wrote:
> Sorry, asked the wrong question.  In fact, I was interested in how to make
> a field unique. I thought that there is the annotation like @Unique or
> something like this. Now I know that it is not.
>
> In any case, thanks for the great plugin
Reply all
Reply to author
Forward
0 new messages