the way the operator $gt is used is incorrect, this should work
val elemMatch = MongoDBObject("records" -> MongoDBObject("$elemMatch" -> MongoDBObject("n" -> "Name", "age"->MongoDBObject( "$gt"-> 0))))
Domanda
I'm trying to implement an elemMatch query with $gt
and $lt
in Casbah.
"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))
On the val elemMatch
line, I'm seeing this compile-time error:
[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/
Soluzione
the way the operator $gt is used is incorrect, this should work
val elemMatch = MongoDBObject("records" -> MongoDBObject("$elemMatch" -> MongoDBObject("n" -> "Name", "age"->MongoDBObject( "$gt"-> 0))))