使用$ gt和$ lt进行elemMatch查询

时间:2013-10-15 20:17:11

标签: scala casbah

我正在尝试在Casbah中使用$gt$lt实施elemMatch查询。

"Testing $gt and $lt in $elemMatch" should "return results" in { 

val TEST = "test"

val db = MongoClient()(TEST)
val collection = db(TEST)

val obj: JsValue = Json.parse("""{ "records" : [{"n" : "Name", "age": 5}]}""")
val doc: DBObject = JSON.parse(obj.toString).asInstanceOf[DBObject]

collection.insert(doc)

val elemMatch = "records" $elemMatch (MongoDBObject("n" -> "Name", "age" $gt 0))
val results = collection.find(elemMatch, MongoDBObject("_id" -> 1))

val elemMatch行上,我看到了这个编译时错误:

[error] ...\TestElemMatch.scala:51: ')' expected but integer
 literal found.
[error]         val elemMatch = "records" $elemMatch (MongoDBObject("n" -> "Name", "age" -> $gt 0))
                                                                                   ^

http://docs.mongodb.org/manual/reference/operator/query/elemMatch/

1 个答案:

答案 0 :(得分:2)

使用运算符$ gt的方式不正确,这应该可行

  

val elemMatch = MongoDBObject(“records” - > MongoDBObject(“$ elemMatch” - > MongoDBObject(“n” - >“Name”,“age” - > MongoDBObject(“$ gt” - > 0))))