Query DSL

Query DSL

Query DSL provides simple DSL that allows to create query that can be used in collection commands,
    that need to limit, sort specify read preference of their operations

##Simple Queries##

 Simple matchAll query : query() { "$query" : { }}
 Multi-key match query : query("intKey" -> 1, "longKey" -> 2L, "doubleKey" -> 0.1d, "stringKey" -> "pat") { "$query" : { "intKey" : 1 , "longKey" : 2 , "doubleKey" : 0.1 , "stringKey" : "pat"}}
 Multi-key ===, =/= query : query("intKey" === 1, "longKey" ≠ 2L, "doubleKey" =/= 0.1d, "stringKey" -> "pat") { "$query" : { "intKey" : 1 , "longKey" : { "$ne" : 2} , "doubleKey" : { "$ne" : 0.1} , "stringKey" : "pat"}}
 Multi-key >, >=, <, <= : query("intKey" > 1, "longKey" >= 2L, "doubleKey" < 0.1d, "intKey2" <= 33) { "$query" : { "intKey" : { "$gt" : 1} , "longKey" : { "$gte" : 2} , "doubleKey" : { "$lt" : 0.1} , "intKey2" : { "$lte" : 33}}}
 Multi-key within / notWithin query : query("intKey" within(1, 2, 3), "longKey" notWithin(2L, 3L), "doubleKey" within (5L)) { "$query" : { "intKey" : { "$in" : [ 1 , 2 , 3]} , "longKey" : { "$nin" : [ 2 , 3]} , "doubleKey" : { "$in" : [ 5]}}}
 Object key match : query("user" all("first", "last"), "customer" all ("address")) { "$query" : { "user" : { "$all" : [ "first" , "last"]} , "customer" : { "$all" : [ "address"]}}}
 Existence match : query("user" present, "customer" present) { "$query" : { "user" : { "$exists" : true} , "customer" : { "$exists" : true}}}
 Modulo match : query("userCount" %(2, 3)) { "$query" : { "userCount" : { "$mod" : [ 2 , 3]}}}
 BSON Type match : query("userName" ofType BSONType.String, "customer" ofType BSONType.Object) { "$query" : { "userName" : { "$type" : 2} , "customer" : { "$type" : 3}}}
 Regular expression match : query("user" regex ".*", "customer" regex "A.*") { "$query" : { "user" : { "$regex" : "."} , "customer" : { "$regex" : "A."}}}
 Regular expr. options match : query("user" regex(".*", "i"), "customer" regex("A.*", "s")) { "$query" : { "user" : { "$regex" : "." , "$options" : "i"} , "customer" : { "$regex" : "A." , "$options" : "s"}}}
 Query $geoWithin TODO
 Query $geoIntersects TODO
 Query $near TODO
 Query $nearSphere TODO
 Array Element match : query("users" elementMatch("name" =/= "john", "age" -> 7)) { "$query" : { "users" : { "$elemMatch" : { "name" : { "$ne" : "john"} , "age" : 7}}}}
 Array Element match logical : query("users" elementMatch ("name" =/= "john" or "age" -> 7)) { "$query" : { "users" : { "$elemMatch" : { "$or" : [ { "name" : { "$ne" : "john"}} , { "age" : 7}]}}}}
 Array size : query("users" ofSize (7)) { "$query" : { "users" : { "$size" : 7}}}
 Same keys in query : query("intKey" <= 10, "intKey" >= 3) { "$query" : { "$and" : [ { "intKey" : { "$lte" : 10}} , { "intKey" : { "$gte" : 3}}]}}

##Logical Queries##

 Simple and : query("intKey" -> 1 and "longKey" =/= 2L) { "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]}}
 Complex and I. : query("intKey" -> 1 and ("longKey" =/= 2L and "strKey" === "one")) { "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}
 Complex and II. : query(("intKey" -> 1 and "longKey" =/= 2L) and "strKey" === "one") { "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}
 Complex and III. : query(("intKey" -> 1 and "longKey" =/= 2L) and ("strKey" === "one" and "boolKey" -> true)) { "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"} , { "boolKey" : true}]}}
 Simple or : query("intKey" -> 1 or "longKey" =/= 2L) { "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]}}
 Complex or I. : query("intKey" -> 1 or ("longKey" =/= 2L or "strKey" === "one")) { "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}
 Complex or II. : query(("intKey" -> 1 or "longKey" =/= 2L) or "strKey" === "one") { "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}
 Complex or III. : query(("intKey" -> 1 or "longKey" =/= 2L) or ("strKey" === "one" or "boolKey" -> true)) { "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"} , { "boolKey" : true}]}}
 Simple nor : query("intKey" -> 1 nor "longKey" =/= 2L) { "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]}}
 Complex nor I. : query("intKey" -> 1 nor ("longKey" =/= 2L nor "strKey" === "one")) { "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}
 Complex nor II. : query(("intKey" -> 1 nor "longKey" =/= 2L) nor "strKey" === "one") { "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}
 Complex nor III. : query(("intKey" -> 1 nor "longKey" =/= 2L) nor ("strKey" === "one" nor "boolKey" -> true)) { "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"} , { "boolKey" : true}]}}

note: for the negation (not) use it without collectionSyntax prefix. it is here just for avoiding namespace clash in specs2

 Equality not : query(collectionSyntax.not("intKey" -> 1), "longKey" =/= 2L) { "$query" : { "intKey" : { "$ne" : 1} , "longKey" : { "$ne" : 2}}}
 Negation of expression : query(collectionSyntax.not("intKey" >= 1), "longKey" =/= 2L) { "$query" : { "intKey" : { "$not" : { "$gte" : 1}} , "longKey" : { "$ne" : 2}}}
 Combine and and or : query(("intKey" -> 1 or "longKey" =/= 2L) and ("strKey" === "one" or "boolKey" -> true)) { "$query" : { "$and" : [ { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]} , { "$or" : [ { "strKey" : "one"} , { "boolKey" : true}]}]}}
 Combine or and and : query(("intKey" -> 1 and "longKey" =/= 2L) or ("strKey" === "one" and "boolKey" -> true)) { "$query" : { "$or" : [ { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]} , { "$and" : [ { "strKey" : "one"} , { "boolKey" : true}]}]}}

Total for specification QueryBuilderSpec
Finished in3 ms
Results35 examples, 0 failure, 0 error, 4 pending