如何使用 Casbah 在 MongoDB 上按对象 ID 查找?
我正在尝试编写一个查询来通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“_id”通常在 MongoDB 中存储为 ObjectID,而不是字符串...字符串和 ObjectID 是不同的类型,您不能将字符串强制转换为 ObjectId。 ObjectId 在 MongoDB 中也是一种不同的类型,因此 ObjectId(“abcdefgh123”) 与字符串“abcdefgh123”不同。
您需要在 Casbah 中按 ObjectID 进行搜索。试试这个:
"_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: