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

Source

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

Source

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

Source

This dump(Int maxWidth := 60)

endsWith

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

new make()

Creates a standard MongoQ instance.

matchesRegex

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

Str print(Int maxWidth := 60, Str indent := " ")

query

Source

Str:Obj? query()

The underlying query that's being build up.

startsWith

Source

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

Source

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

Source

MongoQ where(Str 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/