How to get the oid a MongoObject in a simple way?

10 views
Skip to first unread message

Freewind

unread,
Mar 19, 2011, 9:09:00 AM3/19/11
to mongo-scala-driver
class User extends MongoObject

val user = new User
collection += user // save
val oid = user.mongoOID.get // get the oid

We need to use `user.mongoOID.get` to get the oid, is there a simpler
way? For example, could you provide a `def oid = this.mongoOID.get` in
`MongoObject`, so we can just use `user.oid` to get it?

I think it's common to get the oid of a object.

Alexander Azarov

unread,
Mar 19, 2011, 2:52:28 PM3/19/11
to mongod...@googlegroups.com
Hello,

If it's so common for your project, use Scala language:

trait EasyOid { self: MongoObject =>
def oid = mongoOID.get
}

and mix it into your classes, e.g.

class User extends MongoObject with EasyOid

That's it.

Regards,
Alexander

19.03.2011 at 16:09, Freewind wrote

李鹏

unread,
Mar 19, 2011, 10:40:10 PM3/19/11
to mongodb-scala
Thank you!
 
 
------------------ Original ------------------
From:  "Alexander Azarov"<aza...@aha.ru>;
Date:  Sun, Mar 20, 2011 02:52 AM
To:  "mongodb-scala"<mongod...@googlegroups.com>;
Subject:  Re: How to get the oid a MongoObject in a simple way?
Reply all
Reply to author
Forward
0 new messages