HOW TO Find by Object ID on MongoDB with Casbah?

10,226

"_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
    })
  }
Share:
10,226

Related videos on Youtube

Remy
Author by

Remy

Updated on May 15, 2022

Comments

  • Remy
    Remy almost 2 years

    I'm trying to write a query to find by Object ID with Casbah, it seems trivial but ... I don't find.

    I tried this:

    def get(id: Option[String]): User = { 
        val mongoDB : MongoDB = MongoConnection().apply("test")
        val mongoColl : MongoCollection = mongoDB.apply("users")
        val objectId = id.getOrElse().asInstanceOf[String]
        val o : DBObject = MongoDBObject("_id" -> objectId)
        val u = mongoColl.findOne(o)
        val user = new User()
        for(x <- u){
             user.id = x.getAs[String]("_id")
             user.username = x.getAs[String]("username")
             user.password = x.getAs[String]("password")
        }
        user
    }
    

    and this:

    def get(id: Option[String]): User = { 
            val mongoDB : MongoDB = MongoConnection().apply("test")
            val mongoColl : MongoCollection = mongoDB.apply("users")
            val objectId = "ObjectId(\"" +id.getOrElse().asInstanceOf[String] + "\")"
            val o : DBObject = MongoDBObject("_id" -> objectId)
            val u = mongoColl.findOne(o)
            val user = new User()
            for(x <- u){
                 user.id = x.getAs[String]("_id")
                 user.username = x.getAs[String]("username")
                 user.password = x.getAs[String]("password")
            }
            user
        }
    

    This compile and run but no result. I also tried this:

    def get(id: Option[String]): User = { 
        val mongoDB : MongoDB = MongoConnection().apply("test")
        val mongoColl : MongoCollection = mongoDB.apply("users")
        val objectId : ObjectId = id.getOrElse().asInstanceOf[ObjectId]
        val o : DBObject = MongoDBObject("_id" -> objectId)
        val u = mongoColl.findOne(o)
        val user = new User()
        for(x <- u){
             user.id = x.getAs[String]("_id")
             user.username = x.getAs[String]("username")
             user.password = x.getAs[String]("password")
        }
        user
    }
    

    But this one doesn't compile because String cannot be cast to ObjectId.

    java.lang.ClassCastException: java.lang.String cannot be cast to org.bson.types.ObjectId
    

    Thank you for your help :)

  • Remy
    Remy about 13 years
    Ok thanks @McAdams. Now it works! def get(id: Option[String]): User = { val mongoDB : MongoDB = MongoConnection().apply("test") val mongoColl : MongoCollection = mongoDB.apply("users") val objectId : ObjectId = new ObjectId(id.getOrElse().asInstanceOf[String]) val user = new User() val o : DBObject = MongoDBObject("_id" -> objectId) val u = mongoColl.findOne(o) for(x <- u){ user.id = x.getAs[String]("_id") user.fullname = x.getAs[String]("fullname") user.username = x.getAs[String]("username") } user }
  • David
    David about 13 years
    You can also do mongoColl.findOne(id.get)