开发者

MongoDB Casbah query field not exist or specific value

开发者 https://www.devze.com 2023-02-25 23:42 出处:网络
I would like to perform a query using casbah in order to 开发者_如何学编程find all objects that have a certain field not set (the field does not exist) or the field has a particular value.

I would like to perform a query using casbah in order to 开发者_如何学编程find all objects that have a certain field not set (the field does not exist) or the field has a particular value.

I have tried using

val query = ("_id.serviceName" $in serviceNames) ++ ($or("element" $exists false), MongoDBObject("element" -> "value"))) 

but I obtain an error:

found com.mongodb.casbah.commons.Imports.DBObject
required (String, Any)

Is it possible to express such query? Thanks


Looks like this may be a bug in the right-hand value filter for $or; it doesn't appear to be accepting a preconstructed DBObject from the $exists DSL statement. It definitely should --- I'm filing a bug internally to fix this; in the meantime you can construct this by doing the "$exists" statement by hand:

scala> val query = ("_id.serviceName" $in serviceNames) ++ ($or(("element" -> MongoDBObject("$exists" -> false)), ("element" -> "value")))            
query: com.mongodb.casbah.commons.Imports.DBObject = { "$or" : [ { "element" : { "$exists" : false}} , { "element" : "value"}] , "_id.serviceName" : { "$in" : [ "foo" , "bar" , "baz" , "bah"]}}

Sorry for the trouble... I've created a bug entry for this to correct for the next release.

0

精彩评论

暂无评论...
验证码 换一张
取 消