Query and selection operators in MongoDB

0 Shares
0
0
0
0

Introduction

Query operators in MongoDB include comparison, logical, elemental, evaluation, geographic, array, bitwise, and comment operators.

MongoDB comparison operators

$eq

The $eq operator specifies an equality condition. This operator matches documents where the value of a field is equal to the specified value.

Syntax:

{ : { $eq: } }

Example:

db.books.find({ price: { $eq: 300 } })

In this example, the query looks at the Books collection to select all documents where the Price field value is 300.

$gt

The $gt operator selects documents in which the field value is greater than the specified value.

Syntax:

{ field: { $gt: value } }

Example:

db.books.find({ price: { $gt: 200 } })
$gte

The $gte operator selects documents where the field value is greater than or equal to the specified value.

Syntax:

{ field: { $gte: value } }

Example:

db.books.find({ price: { $gte: 250 } })
$in

The $in operator selects documents where the value of a field is equal to any of the values in the specified array.

Syntax:

{ field: { $in: [, , …] } }

Example:

db.books.find({ price: { $in: [100, 200] } })
$lt

The $lt operator selects documents where the field value is less than the specified value.

Syntax:

{ field: { $lt: value } }

Example:

db.books.find({ price: { $lt: 20 } })
$lte

The $lte operator selects documents where the field value is less than or equal to the specified value.

Syntax:

{ field: { $lte: value } }

Example:

db.books.find({ price: { $lte: 250 } })
$ne

The $ne operator selects documents where the field value is not equal to the specified value.

Syntax:

{ : { $ne: } }

Example:

db.books.find({ price: { $ne: 500 } })
$nin

The $nin operator selects documents where the field value is not in the specified array or does not exist at all.

Syntax:

{ field: { $nin: [, , …] } }

Example:

db.books.find({ price: { $nin: [50, 150, 200] } })

MongoDB logical operators

$and

The $and operator acts as a logical AND operator on an array. The array must contain one or more expressions and selects documents that satisfy all expressions in the array.

Syntax:

{ $and: [ { }, { }, ....] }

Example:

db.books.find({ $and: [ { price: { $ne: 500 } }, { price: { $exists: true } } ] })
$not

The $not operator acts as a logical NOT operator on the specified expression and selects documents that do not match the corresponding expression.

Syntax:

{ field: { $not: { } } }

Example:

db.books.find({ price: { $not: { $gt: 200 } } })
$nor

The $nor operator acts as a logical NOR operator on an array of one or more query terms, selecting documents that do not satisfy any of the query terms in the array.

Syntax:

{ $nor: [ { }, { }, ..... ] }

Example:

db.books.find({ $nor: [ { price: 200 }, { sale: true } ] })
$or

<pعملگر $or به عنوان یک عملگر منطقی OR روی یک آرایه از دو یا چند عبارت عمل می‌کند و اسنادی را انتخاب می‌کند که حداقل یکی از عبارات را برآورده کنند.

Syntax:

{ $or: [ { }, { }, ... , { } ] }

Example:

db.books.find({ $or: [ { quantity: { $lt: 200 } }, { price: 500 } ] })

MongoDB Elemental Operators

$exists

The $exists operator selects documents that contain the specified field when the boolean value is true. It also selects documents that contain the field when the value is null.

Syntax:

{ field: { $exists: } }

Example:

db.books.find({ qty: { $exists: true, $nin: [5, 15] } })
$type

The $type operator selects documents whose field value is specified as BSON type.

Syntax:

{ field: { $type: } }

Example:

db.books.find({ "bookid": { $type: 2 } })

MongoDB evaluation operators

$expr

The $expr operator allows the use of aggregate expressions in the query language.

Syntax:

{ $expr: { } }

Example:

db.store.find({ $expr: { $gt: [ "$product", "price" ] } })
$jsonSchema

This operator selects documents that match the specified JSON schema.

Syntax:

{ $jsonSchema: }
$mod

The $mod operator selects documents whose field value, when divided by the divisor, has the specified remainder.

Syntax:

{ field: { $mod: [ divisor, remainder ] } }

Example:

db.books.find({ qty: { $mod: [ 200, 0 ] } })
$regex

This operator provides regular expression capabilities for matching string patterns in queries. MongoDB uses Perl-compatible regular expressions.

Syntax:

{ : /pattern/ }

Example:

db.books.find({ price: { $regex: /789$/ } })
$text

The $text operator searches the contents of a field that is indexed with a text index.

Syntax:

{ $text: { $search: , $language: , $caseSensitive: , $diacriticSensitive: } }

Example:

db.books.find({ $text: { $search: "Othelo" } })
$where

The “where” operator is used to pass a string containing a JavaScript expression or a complete JavaScript function to the query engine.

Example:

db.books.find({ $where: function() { return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a8") } })
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like