2011-04-07 11 views
8

Casbah ile Nesne Kimliği ile bulmak için bir sorgu yazmaya çalışıyorum, önemsiz gibi görünüyor ama ... Ben bulamıyorum.MOBDB'de Casbah ile Nesne Kimliği ile Nasıl Bulunur?

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 
} 

ve bu:

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 
    } 

Bu derleme ve koşmak ama hiçbir sonuç

Bu çalıştı. Ben de bu çalıştı:

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 
} 

Ama Dize objectId için döküm edilemez çünkü bu bir derleme değil.

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

Yardımlarınız :) için teşekkür ederiz

cevap

12

"_ID" tipik MongoDB bir NesneKimliği olarak saklanır ve bir dize ... Dize ve NesneKimliği farklı türleri vardır ve bir için bir Dize döküm olamaz edilir Nesne Kimliği. ObjectId de MongoDB içinde ayrı bir türüdür, bu yüzden ObjectId ("abcdefgh123") "abcdefgh123" dizesiyle aynı DEĞİLDİR.

Casasa'da ObjectID'e göre arama yapmanız gerekmektedir. Bunu yerine deneyin:

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 
    }) 
    } 
+1

Ok teşekkürler @McAdams. Şimdi çalışıyor! . Kullanıcı = { val mongoDB: 'def (: Opsiyon [dize] id) almak MongoDB = MongoConnection() uygulanır ("test") val mongoColl: MongoCollection = mongoDB.apply ("kullanıcılar") val objectId : ObjectId = new ObjectId (id.getOrElse(). AsInstanceOf [String]) val kullanıcı = new Kullanıcı() val o: DBObject = MongoDBObject ("_ id" -> objectId) val u = mongoColl.findOne (o) için (x <- u) { user.id = x.getAs [String] ("_ id") user.fullname = x.getAs [String] ("ad)" user.username = x.getAs [ Dize] ("kullanıcı adı") } kullanıcı } ' – Remy

+1

Ayrıca şunları da yapabilirsiniz: mongoColl.findOne yapın (id.get) – David

İlgili konular