Created
October 3, 2013 21:23
-
-
Save kevinmeredith/6817354 to your computer and use it in GitHub Desktop.
Testing Mongo's $elemMatch DSL with the Casbah API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test | |
| import com.mongodb.casbah.Imports._ | |
| import com.mongodb.util.JSON | |
| import com.typesafe.config.ConfigFactory | |
| import org.scalatest._ | |
| import org.scalatest.matchers.ShouldMatchers | |
| import play.api.libs.json._ | |
| import play.api.{Play, Logger} | |
| class TestElemMatch extends FlatSpec with ShouldMatchers { | |
| "Testing $elemMatch" should "return results" { | |
| val collection = MongoConnection()("test")("test") | |
| val obj: JsValue = Json.parse("""records : [{n : "Name", v: "Will"}]""") | |
| val doc: DBObject = JSON.parse(obj.toString).asInstanceOf[DBObject] | |
| collection.insert(doc) | |
| val elemMatch = "records" $elemMatch (MongoDBObject("n" -> "Name", "v" -> "Will")) | |
| var builder = MongoDBObject() | |
| builder = builder ++ elemMatch | |
| collection.find(builder) | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[playApp] $ test-only test.TestElemMatch
[info] Compiling 2 Scala sources
[error] TestElemMatch.scala:27: type mismatch
[error] found : collection.CursorType
[error](which expands to) com.mongodb.casbah.MongoCursor
[error] required: Int
[error] collection.find(builder)
[error] ^
[error] one error found
error Compilation failed
[error] Total time: 1 s, completed Oct 3, 2013 5:21:25 PM