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##
query()
{ "$query" : { }}query("intKey" -> 1, "longKey" -> 2L, "doubleKey" -> 0.1d, "stringKey" -> "pat")
{ "$query" : { "intKey" : 1 , "longKey" : 2 , "doubleKey" : 0.1 , "stringKey" : "pat"}}query("intKey" === 1, "longKey" ≠ 2L, "doubleKey" =/= 0.1d, "stringKey" -> "pat")
{ "$query" : { "intKey" : 1 , "longKey" : { "$ne" : 2} , "doubleKey" : { "$ne" : 0.1} , "stringKey" : "pat"}}query("intKey" > 1, "longKey" >= 2L, "doubleKey" < 0.1d, "intKey2" <= 33)
{ "$query" : { "intKey" : { "$gt" : 1} , "longKey" : { "$gte" : 2} , "doubleKey" : { "$lt" : 0.1} , "intKey2" : { "$lte" : 33}}}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]}}}query("user" all("first", "last"), "customer" all ("address"))
{ "$query" : { "user" : { "$all" : [ "first" , "last"]} , "customer" : { "$all" : [ "address"]}}}query("user" present, "customer" present)
{ "$query" : { "user" : { "$exists" : true} , "customer" : { "$exists" : true}}}query("userCount" %(2, 3))
{ "$query" : { "userCount" : { "$mod" : [ 2 , 3]}}}query("userName" ofType BSONType.String, "customer" ofType BSONType.Object)
{ "$query" : { "userName" : { "$type" : 2} , "customer" : { "$type" : 3}}}query("user" regex ".*", "customer" regex "A.*")
{ "$query" : { "user" : { "$regex" : "."} , "customer" : { "$regex" : "A."}}}query("user" regex(".*", "i"), "customer" regex("A.*", "s"))
{ "$query" : { "user" : { "$regex" : "." , "$options" : "i"} , "customer" : { "$regex" : "A." , "$options" : "s"}}}query("users" elementMatch("name" =/= "john", "age" -> 7))
{ "$query" : { "users" : { "$elemMatch" : { "name" : { "$ne" : "john"} , "age" : 7}}}}query("users" elementMatch ("name" =/= "john" or "age" -> 7))
{ "$query" : { "users" : { "$elemMatch" : { "$or" : [ { "name" : { "$ne" : "john"}} , { "age" : 7}]}}}}query("users" ofSize (7))
{ "$query" : { "users" : { "$size" : 7}}}query("intKey" <= 10, "intKey" >= 3)
{ "$query" : { "$and" : [ { "intKey" : { "$lte" : 10}} , { "intKey" : { "$gte" : 3}}]}}##Logical Queries##
query("intKey" -> 1 and "longKey" =/= 2L)
{ "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]}}query("intKey" -> 1 and ("longKey" =/= 2L and "strKey" === "one"))
{ "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}query(("intKey" -> 1 and "longKey" =/= 2L) and "strKey" === "one")
{ "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}query(("intKey" -> 1 and "longKey" =/= 2L) and ("strKey" === "one" and "boolKey" -> true))
{ "$query" : { "$and" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"} , { "boolKey" : true}]}}query("intKey" -> 1 or "longKey" =/= 2L)
{ "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]}}query("intKey" -> 1 or ("longKey" =/= 2L or "strKey" === "one"))
{ "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}query(("intKey" -> 1 or "longKey" =/= 2L) or "strKey" === "one")
{ "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}query(("intKey" -> 1 or "longKey" =/= 2L) or ("strKey" === "one" or "boolKey" -> true))
{ "$query" : { "$or" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"} , { "boolKey" : true}]}}query("intKey" -> 1 nor "longKey" =/= 2L)
{ "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}}]}}query("intKey" -> 1 nor ("longKey" =/= 2L nor "strKey" === "one"))
{ "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}query(("intKey" -> 1 nor "longKey" =/= 2L) nor "strKey" === "one")
{ "$query" : { "$nor" : [ { "intKey" : 1} , { "longKey" : { "$ne" : 2}} , { "strKey" : "one"}]}}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
query(collectionSyntax.not("intKey" -> 1), "longKey" =/= 2L)
{ "$query" : { "intKey" : { "$ne" : 1} , "longKey" : { "$ne" : 2}}}query(collectionSyntax.not("intKey" >= 1), "longKey" =/= 2L)
{ "$query" : { "intKey" : { "$not" : { "$gte" : 1}} , "longKey" : { "$ne" : 2}}}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}]}]}}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 in | 3 ms |
Results | 35 examples, 0 failure, 0 error, 4 pending |