如何使用 Casbah 在 MongoDB 上按对象 ID 查找?

发布于 2024-10-31 07:27:53 字数 2017 浏览 0 评论 0原文

我正在尝试编写一个查询来通过 Casbah 的对象 ID 进行查找,这看起来很简单,但是......我没有找到。

我尝试了这个:

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
}

和这个:

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
    }

编译并运行但没有结果。 我也尝试过这个:

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
}

但是这个无法编译,因为 String 无法转换为 ObjectId。

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

感谢您的帮助 :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So尛奶瓶 2024-11-07 07:27:53

“_id”通常在 MongoDB 中存储为 ObjectID,而不是字符串...字符串和 ObjectID 是不同的类型,您不能将字符串强制转换为 ObjectId。 ObjectId 在 MongoDB 中也是一种不同的类型,因此 ObjectId(“abcdefgh123”) 与字符串“abcdefgh123”不同。

您需要在 Casbah 中按 ObjectID 进行搜索。试试这个:

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

"_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
    })
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文