YouTip LogoYouTip

Mongodb Operators

MongoDB Query Operators

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

In MongoDB, query operators are used to filter, compare, and perform logical operations when querying documents.

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

Comparison Operators

Comparison operators include:

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 { age: { $gte: 25 } }
$lt Less than { age: { $lt: 25 } }
$lte Less than or equal { 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)

Syntax:

{ field: { $eq: value } }

Find documents where age equals 25:

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

$ne (Not equal)

Syntax:

{ field: { $ne: value } }

Find documents where age is not equal to 25:

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

$gt (Greater than)

Syntax:

{ field: { $gt: value } }

Find documents where age is greater than 25:

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

$gte (Greater than or equal)

Syntax:

{ field: { $gte: value } }

Find documents where age is greater than or equal to 25:

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

$lt (Less than)

Syntax:

{ field: { $lt: value } }

Find documents where age is less than 25:

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

$lte (Less than or equal)

Syntax:

{ field: { $lte: value } }

Find documents where age is less than or equal to 25:

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

$in (Value in array)

Syntax:

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

Find documents where age is among 20, 25, and 30:

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

$nin (Value not in array)

Syntax:

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

Find documents where age is not among 20, 25, and 30:

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

Logical Operators

Logical operators include:

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

$and (All conditions must be true)

Syntax:

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

Find documents where age is greater than 25 and less than 35:

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

$or (At least one condition is true)

Syntax:

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

Find documents where age is greater than 25 or less than 18:

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

$not (Negation condition)

Syntax:

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

Find documents where age is not greater than 25:

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

$nor (No condition is true)

Syntax:

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

Find documents where age is not greater than 25 and not less than 18:

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

Element Operators

Element operators include:

Operator Description Example
$exists Whether the field exists { age: { $exists: true } }
$type 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 type integer:

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

Array Operators

Array operators include:

Operator Description Example
$all Array contains all specified elements { tags: { $all: ["red", "blue"] } }
$elemMatch Array element matches specified condition { results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } }
$size Array length equals specified value { tags: { $size: 3 } }

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

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

$all (Array contains all specified elements)

Syntax:

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

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

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

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

Syntax:

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

Find documents where an element in the items array has a quantity greater than 10:

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

$size (Array length)

Syntax:

{ field: { $size: value } }

Find documents where the tags array has exactly 3 elements:

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

Other Operators

There are also some other operators as follows:

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

db.collection.find({ location: { $geoWithin: { $centerSphere: [[10, 20], 1] } } })
← Mongodb Operators TypeMongodb Query β†’