MonsterDB – Operators

Make your data beautiful again.

Operators

Operators allow you to specify the behaviour of the system, like using logical statements (or, and) expressions are multiple function type statements built up in a document structure.

$or

Or operator allows you to specify multiple conditions that will be evaluated

CLI Usage: db.collection.find({"$or": [{"LastName":"Haynes"}, {"LastName":"Smith"}]}]

 

API Usage: cursor = collection.find(document)

where document is equivalent to {“$or”: [{“LastName”:”Haynes”}, {“LastName”:”Smith}]}

 

$and

Or operator allows you to specify multiple conditions that will be evaluated

CLI Usage: db.collection.find({"$and": [{"FirstName":"Bob"}, {"LastName":"Smith"}]}]

API Usage: cursor = collection.find(document)
where document is equivalent to

{"$and": [{"FirstName":"Bob"}, {"LastName":"Smith}]}

Please be aware that the above statement will not evaluate to the same as a simple query in one document: {“FirstName”:”Bob”, “LastName”:”Smith} 

$date

Replaces the property with the current date and time

{currentDate: $date}

$month

Extracts the month from the property value indicated

{currentMonth: {$month: somevalue/expression}}

$dayOfMonth

extracts the day of the month from the property value indicated

{currentDay: {$dayOfMonth: somevalue/expression}}

$year

Extracts the year from the property value indicated:

{currentYear: {$year: somevalue/expression}}

$multiply

Multiply the values in the supplied list

{multipliedValues: {$multiply: [1,2]}}

$divide

Divide the values in the supplied list.

{dividedValues: {$divide: [1,2,3]}}

$gt

Test to see if the values supplied (numeric only) are the greater, doubles will be compared to integers by converting the integer to a double first.

{result: {SomeValue: {$gt: 1}}}

$lt

Test to see if the values supplied (numeric only) are the lesser than, doubles will be compared to integers by converting the integer to a double first.

{result: {SomeValue: {$gt: 1}}}

$eq

Test to see if the values supplied are the equal, doubles will be compared to integers by converting the integer to a double first. Strings will be compared case sensitively.

{result: {SomeValue: {$eq: "1"}}}

$equalsIgnoreCase

Test to see if the values supplied are strings and the same ignoring case

{result: {SomeValue: {$equalsIgnoreCase: “1”}}}

$contains

Test to see if the the values are contained within a string.

{result: {SomeValue: {$contains: ["hello", "ell"]}}}

$startsWith

Test to see if the values supplied are started by a value

{result: {SomeValue: {$startsWith: "1"}}}

$endsWith

Test to see if the values supplied are ended by the value

{result: {SomeValue: {$endsWith: "1"}}}

$ne

Test to see if the values supplied (numeric only) are the equal, doubles will be compared to integers by converting the integer to a double first.

{result: {SomeValue: {$ne: 1}}}

$popFirst

returns the first value from a list

{result: {$popFirst: alist}}

$popLast

returns the last value from a list

{result: {$popLast: alist}}

$upper

returns the uppercase value

{result: {$upper: "aaaa"}}

$lower

returns the lowercase value

{result: {$lower: "AAA"}}

 

Regular Expressions

Regular expression can be used in filters and find/$match operators. Regular expressions are evaluated using java, so will conform to the rules:

A regular expression, or regex, is a sequence of characters that specifies a pattern which can be searched for in a text. A regex defines a set of strings, usually united for a given purpose. Suppose you need a way to formalize and refer to all the strings that make up the format of an email address. Since there are a near infinite number of possible email addresses, it’d be hard to enumerate them all. However, as we know an email address has a specific structure, and we can encode that using the regex syntax.

A regex processor translates a regular expression into an internal representation which can be executed and matched against the text being searched. It will tell you whether a string is in the set of strings defined by a pattern or find a substring that belongs in that set.

Example in CLI

{"pattern":"Rob.*", flags":0}

Using java.util.regex.Pattern in the API:

Pattern.compile("Rob.*")

 

 

$geoWithin

Allows you to query a collection for documents that fit within the specified geo shape, such as polygon, line, linestring, center, point etc.

db.collection.find({location: {$geoWithin: { type: "Point", coordinates: [ 30, 5 ] } }} )

More information found in the geospatial section.

Fork me on GitHub