HOW TO Find by Object ID on MongoDB with Casbah?
"_id" is typically stored as an ObjectID in MongoDB and not a String... String and ObjectID are different types and you cannot cast a String to an ObjectId. ObjectId is a distinct type within MongoDB as well, so ObjectId("abcdefgh123") is NOT the same as the String "abcdefgh123".
You need to search by ObjectID here within Casbah. Try this instead:
def get(id: Option[ObjectId]): User = { val mongoDB : MongoDB = MongoConnection().apply("test") val mongoColl : MongoCollection = mongoDB.apply("users") val objectId : ObjectId = id.getOrElse().asInstanceOf[ObjectId] id.foreach( oid => { val o : DBObject = MongoDBObject("_id" -> oid) val u = mongoColl.findOne(o) val user = new User() for(x <- u){ user.id = x.getAs[ObjectId]("_id") user.username = x.getAs[String]("username") user.password = x.getAs[String]("password") } user }) }