title: HOW TO Find by Object ID on MongoDB with Casbah?
body: <p>I'm trying to write a query to find by Object ID with Casbah, it seems trivial but ... I don't find.</p>
  
  <p>I tried this:</p>
  
  <pre><code>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" -&gt; objectId)
      val u = mongoColl.findOne(o)
      val user = new User()
      for(x &lt;- u){
           user.id = x.getAs[String]("_id")
           user.username = x.getAs[String]("username")
           user.password = x.getAs[String]("password")
      }
      user
  }
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>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" -&gt; objectId)
          val u = mongoColl.findOne(o)
          val user = new User()
          for(x &lt;- u){
               user.id = x.getAs[String]("_id")
               user.username = x.getAs[String]("username")
               user.password = x.getAs[String]("password")
          }
          user
      }
  </code></pre>
  
  <p>This compile and run but no result.
  I also tried this:</p>
  
  <pre><code>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" -&gt; objectId)
      val u = mongoColl.findOne(o)
      val user = new User()
      for(x &lt;- u){
           user.id = x.getAs[String]("_id")
           user.username = x.getAs[String]("username")
           user.password = x.getAs[String]("password")
      }
      user
  }
  </code></pre>
  
  <p>But this one doesn't compile because String cannot be cast to ObjectId.</p>
  
  <pre><code>java.lang.ClassCastException: java.lang.String cannot be cast to org.bson.types.ObjectId
  </code></pre>
  
  <p>Thank you for your help :)</p>