【发布时间】:2013-09-18 01:42:56
【问题描述】:
我正在学习 Scala 并尝试使用 Mongo。我正在创建一个接收Map[String, Any] 作为参数的函数,我想为它返回正确的MongoDBObject:
def parse(query: Map[String, Any]): MongoDBObject = {
val result = query("operation") match {
case "all" => query("field").toString $all query("value").asInstanceOf[List[String]]
case "in" => query("field").toString $in query("value").asInstanceOf[List[String]]
case "regex" => query("field").toString $regex query("value")
case "eq" => query("field").toString $eq query("value")
case "gt" => query("field").toString $gt query("value")
case "gte" => query("field").toString $gte query("value")
case "lt" => query("field").toString $lt query("value")
case "lte" => query("field").toString $lte query("value")
case "exists" => query("field").toString $exists query("value").asInstanceOf[Boolean]
case "size" => query("field").toString $size query("value").asInstanceOf[Int]
case "where" => $where(query("value").toString)
case _ => throw new NotImplementedError("Unknown operation")
}
}
我有一些问题。
- 编译器说
$regex不是String的成员。我不知道为什么。 - 编译器说
Any不是一个有效的查询参数。我想我应该转换为 int、string、date 或任何其他有效的 Mongo 类型。有没有什么方法可以在没有反射的情况下解决这个问题来解决这个值的类型? - 对于
$mod操作,我应该给出两个数值作为参数。我应该使用List作为地图的值并获取第一项和第二项吗?
【问题讨论】: