Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MongoDB Conditional Operators

MongoDB Conditional Operators

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

In MongoDB, conditional operators are used for filtering, comparison, and 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:

Operator Description Example
$eq Equal { age: { $eq: 25 } }
$ne Not equal { age: { $ne: 25 } }
$gt Greater than { age: { $gt: 25 } }
$gte Greater than or equal to { age: { $gte: 25 } }
$lt Less than { age: { $lt: 25 } }
$lte Less than or equal to { age: { $lte: 25 } }
$in In the specified array { age: { $in: [25, 30, 35] } }
$nin Not in the specified array { age: { $nin: [25, 30, 35] } }

$eq (equal to)

Syntax format:

{ field: { $eq: value } }

Find people whose age equals 25:



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

$ne (not equal to)

Syntax format:

{ field: { $ne: value } }

Find people whose age does not equal 25:



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

$gt (greater than)

Syntax format:

{ field: { $gt: value } }

Find people whose age is greater than 25:



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

$gte (greater than or equal to)

Syntax format:

{ 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 format:

{ 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 format:

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

Find people whose age is 20, 25, or 30:



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

$nin (value is not in the array)

Syntax format:

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

Find people whose age is not 20, 25, or 30:



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


Logical operators

The logical operators are:

Operator Description Example
$and Logical AND, satisfying all conditions { $and: [ { age: { $gt: 25 } }, { city: "New York" } ] }
$or Logical OR, satisfying any condition { $or: [ { age: { $lt: 25 } }, { city: "New York" } ] }
$not Negation, not satisfying the condition { age: { $not: { $gt: 25 } } }
$nor Logical NOR, satisfying none of the conditions { $nor: [ { age: { $gt: 25 } }, { city: "New York" } ] }

$and (multiple conditions all hold)

Syntax format:

{ $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 format:

{ $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 format:

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

Find people whose age is not greater than 25:



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

$nor (no condition holds)

Syntax format:

{ $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:

Operator Description Example
$exists Whether a field exists { age: { $exists: true } }
$type The BSON type of a field { age: { $type: "int" } }

$exists (whether a field exists)

Syntax format:

{ field: { $exists: boolean } }

Find documents that contain the age field:



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

$type (field type)

Syntax format:

{ 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:

Operator Description Example
$all The array contains all the specified elements { tags: { $all: ["red", "blue"] } }
$elemMatch Elements in the array match the specified condition { results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } }
$size The length of the array equals the specified value { tags: { $size: 3 } }

Find documents where the tags array contains "red" and "blue":



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

$all (the array contains all the specified elements)

Syntax format:

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

Find documents where the tags array contains "mongodb" and "database":



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

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

Syntax format:

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

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



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

$size (the size of the array)

Syntax format:

{ 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:

Operator Description Example
$regex Match a regular expression { name: { $regex: /^A/ } }
$text Perform a text search { $text: { $search: "coffee" } }
$where Use a JavaScript expression for conditional filtering { $where: "this.age > 25" }
$near Find documents near a specified point db.collection.find({ location: { $near: [10, 20], $maxDistance: 1000 } })
$geoWithin Find documents within a specified geographic area db.collection.find({ location: { $geoWithin: { $centerSphere: [[10, 20], 1] } } })

$regex (regular expression match)

Syntax format:

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

Find documents where name starts with "John":



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

$near (documents near a specified point)

Syntax format:

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

Find documents near a certain location:



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

$geoWithin (find documents within a specified area)

Syntax format:

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

Find documents within a specified geographic area:



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