Skip to content

Instantly share code, notes, and snippets.

@kevinmeredith
Created October 3, 2013 21:23
Show Gist options
  • Select an option

  • Save kevinmeredith/6817354 to your computer and use it in GitHub Desktop.

Select an option

Save kevinmeredith/6817354 to your computer and use it in GitHub Desktop.
Testing Mongo's $elemMatch DSL with the Casbah API
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)
}
}
@kevinmeredith

Copy link
Copy Markdown
Author

[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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment