classafMongo::MongoQ
sys::Obj afMongo::MongoQ
A means to build common Mongo queries with sane objects and methods. (And not some incomprehensible mess of nested maps and lists!)
query := MongoQ { and( or( eq("price", 0.99f), eq("price", 1.99f) ), or( eq("sale", true), lessThan("qty", 29) ) ) }.query
- and
MongoQ and(MongoQ q1, MongoQ q2, MongoQ? q3 := null, MongoQ? q4 := null)
Selects documents that pass all the query expressions in the given list.
q.and( q.lessThan("quantity", 20), q.eq("price", 10) )
@see https://www.mongodb.com/docs/manual/reference/operator/query/and/
- contains
MongoQ contains(Obj name, Str value, Bool caseInsensitive := true)
Matches string values that contain the given value. Matching is performed with regular expressions.
q.contains("name", "Em")
@see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
- dump
- endsWith
MongoQ endsWith(Obj name, Str value, Bool caseInsensitive := true)
Matches string values that end with the given value. Matching is performed with regular expressions.
q.endsWith("name", "ma")
@see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
- eq
MongoQ eq(Obj name, Obj? value)
Matches values that are equal to the given object.
q.eq("score", 11)
Shorthand notation.
q->score = 11
- eqIgnoreCase
MongoQ eqIgnoreCase(Obj name, Str value)
Matches string values that equal (ignoring case) the given value. Matching is performed with regular expressions.
q.eqIgnoreCase("name", "emm?")
@see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
- exists
MongoQ exists(Obj name, Bool exists := true)
Matches if the field exists (or not), even if it is
null
.q.exists("score")
@see https://www.mongodb.com/docs/manual/reference/operator/query/exists/
- greaterThan
MongoQ greaterThan(Obj name, Obj value)
Matches values that are greater than the given object.
q.greaterThan("score", 8)
@see https://www.mongodb.com/docs/manual/reference/operator/query/gt/
- greaterThanOrEqTo
MongoQ greaterThanOrEqTo(Obj name, Obj value)
Matches values that are greater than or equal to the given object.
q.greaterThanOrEqTo("score", 8)
@see https://www.mongodb.com/docs/manual/reference/operator/query/gte/
- in
MongoQ in(Obj name, Obj[] values)
Matches values that equal any one of the given values.
q.in("score", [9, 10, 11])
@see https://www.mongodb.com/docs/manual/reference/operator/query/in/
- lessThan
MongoQ lessThan(Obj name, Obj value)
Matches values that are less than the given object.
q.lessThan("score", 5)
@see https://www.mongodb.com/docs/manual/reference/operator/query/gt/
- lessThanOrEqTo
MongoQ lessThanOrEqTo(Obj name, Obj value)
Matches values that are less than or equal to the given object.
q.lessThanOrEqTo("score", 5)
@see https://www.mongodb.com/docs/manual/reference/operator/query/lte/
- make
new make()
Creates a standard MongoQ instance.
- matchesRegex
MongoQ matchesRegex(Obj name, Regex regex)
Matches string values that equal the given regular expression.
q.matchesRegex("name", "Emm?")
@see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
- mod
MongoQ mod(Obj name, Int divisor, Int remainder)
Matches values based on their remainder after a division (modulo operation).
q.mod("score", 3, 0)
@see https://www.mongodb.com/docs/manual/reference/operator/query/mod/
- nor
MongoQ nor(MongoQ q1, MongoQ q2, MongoQ? q3 := null, MongoQ? q4 := null)
Selects documents that fail all the query expressions in the given list.
query := nor( lessThan("quantity", 20), eq("price", 10) )
@see https://www.mongodb.com/docs/manual/reference/operator/query/nor/
- not
MongoQ not(MongoQ query := this)
Selects documents that do not match the given following criterion. Example:
not.eq("score", 11) eq("score", 11).not not(q.eq("score", 11))
@see https://www.mongodb.com/docs/manual/reference/operator/query/not/
- notEq
MongoQ notEq(Obj name, Obj? value)
Matches values that are not equal to the given object.
Note this also matches documents that do not contain the field.
q.notEq("score", 11)
@see https://www.mongodb.com/docs/manual/reference/operator/query/ne/
- notIn
MongoQ notIn(Obj name, Obj[] values)
Matches values that do not equal any one of the given values.
Note this also matches documents that do not contain the field.
q.notIn("score", [1, 2, 3])
@see https://www.mongodb.com/docs/manual/reference/operator/query/nin/
- or
MongoQ or(MongoQ q1, MongoQ q2, MongoQ? q3 := null, MongoQ? q4 := null)
Selects documents that pass any of the query expressions in the given list.
query := or( lessThan("quantity", 20), eq("price", 10) )
@see https://www.mongodb.com/docs/manual/reference/operator/query/or/
- print
- query
The underlying query that's being build up.
- startsWith
MongoQ startsWith(Obj name, Str value, Bool caseInsensitive := true)
Matches string values that start with the given value. Matching is performed with regular expressions.
q.startsWith("name", "Em")
@see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
- textSearch
MongoQ textSearch(Str search, [Str:Obj?]? opts := null)
Performs a text search on the collection.
Text searching makes use of stemming and ignores language stop words. Quotes may be used to search for exact phrases and prefixing a word with a hyphen-minus (-) negates it.
To enable text searching, ensure the Collection has a text Index else MongoDB will throw an Err.
To sort by search relevance, add the following projection AND sort.
col.find(MongoQ().textSearch("quack").query ) { it->projection = ["_textScore": ["\$meta": "textScore"]] it->sort = ["_textScore": ["\$meta": "textScore"]] }
options
may include the following:Name
Type
Desc
$language
Bool
Determines the list of stop words for the search and the rules for the stemmer and tokenizer. See Supported Text Search Languages. Specify
none
for simple tokenization with no stop words and no stemming. Defaults to the language of the index.$caseSensitive
Bool
Enable or disable case sensitive searching. Defaults to
false
.$diacriticSensitive
Int
Enable or disable diacritic sensitive searching. Defaults to
false
.@see https://docs.mongodb.com/manual/reference/operator/query/text/.
- where
Selects documents based on the return value of a javascript function. Example:
q.where("this.name == 'Judge Dredd'")
Only 1 where function is allowed per query.
@see https://www.mongodb.com/docs/manual/reference/operator/query/where/