Scala 带有$gt和$lt的elemMatch查询
我正在尝试在Casbah中使用Scala 带有$gt和$lt的elemMatch查询,scala,casbah,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}
$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))
^
操作员$gt的使用方式不正确,应该可以使用 val elemMatch=MongoDBObject(“记录”->MongoDBObject($elemMatch”->MongoDBObject(“n”->“名称”,“年龄”->MongoDBObject($gt”->0)))
谢谢,这很有效。如何使用$gt和$lt?只需添加另一个条件:val elemMatch=MongoDBObject(“记录”->MongoDBObject($elemMatch”->MongoDBObject(“n”->“名称”,“年龄”->MongoDBObject($gt”->0),“年龄”->MongoDBObject($lt”->6)))