scala - MongoDB cross-collection query with casbah -
i have 2 collections follwoing,
customers: {id: 1, name: "foo"} {id: 2, name: "bar"} {id: 3, name: "baz"} flags: {cid: 1} {cid: 3}
then retrieving customers flag on
db.customers.find({id: {$in: db.flags.distinct("cid", {})}})
on shell works, can't same using casbah, since casbah not seem support querying function call or local variable.
of course can in casbah - remember db.flags.distinct
returns iterable should converted implicitly list use in $in
. heres test example you:
import com.mongodb.casbah.imports._ val db = mongoclient()("casbahtest") val customers = db("customers") val flags = db("flags") customers.drop() flags.drop() // add customers customers += mongodbobject("_id" -> 1, "name" -> "foo") customers += mongodbobject("_id" -> 2, "name" -> "bar") customers += mongodbobject("_id" -> 3, "name" -> "baz") // add 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 build mongodbobject: customers.find("_id" $in flags.distinct("cid"))
Comments
Post a Comment