I'm learning Scala and trying Mongo too. I'm creating a function that receives a Map[String, Any]
as a parameter and I would like to return the proper MongoDBObject
for it:
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")
}
}
I have some issues.
- the compiler says
$regex
is not a member ofString
. I don't know why. - the compiler says that
Any
is not a valid query parameter. I suppose I should cast to int, string, date or any other valid Mongo type. Is there any way to fix this without reflection to solve wich type the value is? - for the
$mod
operation I should give two numeric values as parameteres. Should I use aList
as value for the map and get first and second items?