MongoDB Query Operators

In this chapter, we will discuss how to use query operators in MongoDB.

In MongoDB, query operators are used for filtering, comparing, and performing logical operations when querying documents.

These operators can be divided into the following categories: comparison operators, logical operators, element operators, array operators, and other commonly used operators.

Comparison Operators

The comparison operators are:

OperatorDescriptionExample
$eqequals{ age: { $eq: 25 } }
$nenot equals{ age: { $ne: 25 } }
$gtgreater than{ age: { $gt: 25 } }
$gtegreater than or equal to{ age: { $gte: 25 } }
$ltless than{ age: { $lt: 25 } }
$lteless than or equal to{ age: { $lte: 25 } }
$inis in the specified array{ age: { $in: [25, 30, 35] } }
$ninis not in the specified array{ age: { $nin: [25, 30, 35] } }

$eq (equals)

Syntax:

{ field: { $eq: value } }

Find people whose age equals 25:

db.collection.find({ age: { $eq: 25 } })

$ne (not equals)

Syntax:

{ field: { $ne: value } }

Find people whose age is not equal to 25:

db.collection.find({ age: { $ne: 25 } })

$gt (greater than)

Syntax:

{ field: { $gt: value } }

Find people whose age is greater than 25:

db.collection.find({ age: { $gt: 25 } })

$gte (greater than or equal to)

Syntax:

{ field: { $gte: value } }

Find people whose age is greater than or equal to 25:

db.collection.find({ age: { $gte: 25 } })

$lt (less than)

Syntax: { field: { $lt: value } }

Find people whose age is less than 25:

db.collection.find({ age: { $lt: 25 } })

$lte (less than or equal to)

Syntax:

{ field: { $lte: value } }

Find people whose age is less than or equal to 25:

db.collection.find({ age: { $lte: 25 } })

$in (value is in the array)

Syntax:

{ field: { $in: [value1, value2, ...] } }

Find people whose age is one of 20, 25, and 30:

db.collection.find({ age: { $in: [20, 25, 30] } })

$nin (value is not in the array)

Syntax:

{ field: { $nin: [value1, value2, ...] } }

Find people whose age is not one of 20, 25, and 30:

db.collection.find({ age: { $nin: [20, 25, 30] } })

Logical Operators

The logical operators are:

OperatorDescriptionExample
$andLogical AND, meets all conditions{ $and: [ { age: { $gt: 25 } }, { city: "New York" } ] }
$orLogical OR, meets any condition{ $or: [ { age: { $lt: 25 } }, { city: "New York" } ] }
$notNegation, does not meet the condition{ age: { $not: { $gt: 25 } } }
$norLogical NOR, meets none of the conditions{ $nor: [ { age: { $gt: 25 } }, { city: "New York" } ] }

$and (all conditions hold)

Syntax:

{ $and: [ { condition1 }, { condition2 } ] }

Find people whose age is greater than 25 and less than 35:

db.collection.find({ $and: [{ age: { $gt: 25 } }, { age: { $lt: 35 } }] })

$or (at least one condition holds)

Syntax:

{ $or: [ { condition1 }, { condition2 } ] }

Find people whose age is greater than 25 or less than 18:

db.collection.find({ $or: [{ age: { $gt: 25 } }, { age: { $lt: 18 } }] })

$not (negates the condition)

Syntax:

{ field: { $not: { condition } } }

Find people whose age is not greater than 25:

db.collection.find({ age: { $not: { $gt: 25 } } })

$nor (none of the conditions hold)

Syntax:

{ $nor: [ { condition1 }, { condition2 } ] }

Find people whose age is not greater than 25 and not less than 18:

db.collection.find({ $nor: [{ age: { $gt: 25 } }, { age: { $lt: 18 } }] })

Element Operators

The element operators are:

OperatorDescriptionExample
$existswhether the field exists{ age: { $exists: true } }
$typethe BSON type of the field{ age: { $type: "int" } }

$exists (field exists)

Syntax:

{ field: { $exists: boolean } }

Find documents that contain the age field:

db.collection.find({ age: { $exists: true } })

$type (field type)

Syntax:

{ field: { $type: "type" } }

Find documents where the age field is of integer type:

db.collection.find({ age: { $type: "int" } })

Array Operators

The array operators are:

OperatorDescriptionExample
$allArray contains all specified elements{ tags: { $all: ["red", "blue"] } }
$elemMatchArray elements match specified conditions{ results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } }
$sizeArray length equals the specified value{ tags: { $size: 3 } }

Find documents whose tags array contains "red" and "blue":

db.myCollection.find({ tags: { $all: ["red", "blue"] } });

$all (array contains all specified elements)

Syntax:

{ field: { $all: [value1, value2, ...] } }

Find documents whose tags array contains "mongodb" and "database":

db.collection.find({ tags: { $all: ["mongodb", "database"] } })

$elemMatch (at least one element in the array meets the condition)

Syntax:
{ field: { $elemMatch: { condition } } }

Find elements in the items array where quantity is greater than 10:

db.collection.find({ items: { $elemMatch: { quantity: { $gt: 10 } } } })

$size (array size)

Syntax:

{ field: { $size: value } }

Find documents where the tags array has a size of 3:

db.collection.find({ tags: { $size: 3 } })

Other Operators

There are some other operators as follows:

OperatorDescriptionExample
$regexMatches a regular expression{ name: { $regex: /^A/ } }
$textPerforms text search{ $text: { $search: "coffee" } }
$whereUses JavaScript expressions for conditional filtering{ $where: "this.age > 25" }
$nearFind documents near a specified pointdb.collection.find({ location: { $near: [10, 20], $maxDistance: 1000 } })
$geoWithinFind documents within a specified geographical areadb.collection.find({ location: { $geoWithin: { $centerSphere: [[10, 20], 1] } } })

$regex (regular expression match)

Syntax:

{ field: { $regex: "pattern" } }

Find documents where name starts with "John":

db.collection.find({ name: { $regex: /^John/ } })

$near (documents near a specified point)

Syntax:

{ field: { $near: [longitude, latitude], $maxDistance: distance } }

Find documents close to a certain location:

db.collection.find({ location: { $near: [10, 20], $maxDistance: 1000 } })

$geoWithin (find documents within a specified area)

Syntax:

{ field: { $geoWithin: { $centerSphere: [[longitude, latitude], radius] } } }

Find documents within a specified geographical area:

db.collection.find({ location: { $geoWithin: { $centerSphere: [[10, 20], 1] } } })
Other extensions