Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOW TO Find by Object ID on MongoDB with Casbah?

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 :)

like image 803
Remy Avatar asked Apr 07 '11 01:04

Remy


People also ask

Where is MongoDB record by ID?

MongoDB provides a function with the name findById() which is used to retrieve the document matching the 'id' as specified by the user. In order to use findById in MongoDB, find() function is used. If no document is found matching the specified 'id', it returns null.

How do I find the ID of an object?

Find User (Object ID) Select Users. Browse to or search for the desired user, then select the account name to view the user account's profile information. The Object ID is located in the Identity section on the right. Find role assignments by selecting Access control (IAM) in the left menu, then Role assignments.

What is the ObjectId in MongoDB?

An ObjectId is a 12-byte BSON type having the following structure − The first 4 bytes representing the seconds since the unix epoch. The next 3 bytes are the machine identifier. The next 2 bytes consists of process id. The last 3 bytes are a random counter value.

Should I use ObjectId MongoDB?

To your questions: Is it ok to use Mongo's “Object ID” as its unique identifier? Yes, it is intended for this purpose. Making unique IDs can be a pain in sharded environments, so MongoDB does this for you.


1 Answers

"_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
    })
  }
like image 84
Brendan W. McAdams Avatar answered Nov 05 '22 23:11

Brendan W. McAdams