Could you please give me an example of how to use the $all
operator for my two elemMatch
objects?
val elemMatch1 = foo()
val elemMatch2 = bar()
How can I perform the query of $all( elemMatch1, elemMatch2)
(all documents where elemMatch1 and elemMatch2)?
I'm not sure how much sense it makes to mix $all
and $elemMatch
but from the docs $all
follows:
{ <field>: { $all: [ <value> , <value1> ... ] }
$elemMatch follows:
{ array: { $elemMatch: <document> } }
Unfortunately, the casbah DSL won't help there as $all
requires a list and $elemMatch
expects a string, so you have to manually have to build the document:
import com.mongodb.casbah.Imports._
val coll = MongoClient()("test")("testB")
coll += MongoDBObject("array" -> List(
MongoDBObject("value1" -> 1, "value2" -> 0),
MongoDBObject("value1" -> 1, "value2" -> 2)
))
val elemMatch = MongoDBObject("$elemMatch" -> MongoDBObject("value1" -> 1, "value2" -> 2))
val query = "array" $all List(elemMatch)
coll.find(query).count