Of course you can do this in casbah - remember db.flags.distinct
returns an iterable that should be converted implicitly to a list for use in $in
. Heres a test example for you:
import com.mongodb.casbah.Imports._
val db = MongoClient()("casbahTest")
val customers = db("customers")
val flags = db("flags")
customers.drop()
flags.drop()
// Add some customers
customers += MongoDBObject("_id" -> 1, "name" -> "foo")
customers += MongoDBObject("_id" -> 2, "name" -> "bar")
customers += MongoDBObject("_id" -> 3, "name" -> "baz")
// Add some flags
flags += MongoDBObject("cid" -> 1)
flags += MongoDBObject("cid" -> 3)
// Query
// In JS: db.customers.find({id: {$in: db.flags.distinct("cid", {})}})
// Long hand:
customers.find(MongoDBObject("_id" -> MongoDBObject("$in" -> flags.distinct("cid"))))
// Using Casbahs query dsl to build the MongoDBObject:
customers.find("_id" $in flags.distinct("cid"))