Logical, Comparison, Element and Array Query Operators with examples in MongoDB

Comparison operators in MongoDB

To compare our data mongodb provide us with multiple comparison operators, we will takes alot at them with examples.

$eq

Matches values that are equal to a specified value.

db.collection.find({field: {$eq: value}})

Example of $eq

Select all the documents inside the articles collection where the name field is equal to "article 1".

db.articles.find({name: {$eq: "article 1"}})

Equivalent

db.articles.find({name: "article 1"})

$ne

Matches all values that are not equal to a specified value.

db.collection.find({field: {$ne: value}})

Example of $ne

Select all the documents inside the articles collection where the name field is not equal to "article 1".

db.articles.find({name: {$ne: "article 1"}})

$gt

Matches values that are greater than a specified value.

db.collection.find({field: {$gt: value}})

Example of $gt

Select all the documents inside the articles collection where the number of views is greater than 20.

db.articles.find({views: {$gt: 20}})

$gte

Matches values that are greater than or equal to a specified value.

db.collection.find({field: {$gte: value}})

Example of $gte

Select all the documents inside the articles collection where the number of views is greater than or equal 20.

db.articles.find({views: {$gte: 20}})

$lt

Matches values that are less than a specified value.

db.collection.find({field: {$lt: value}})

Example of $lt

Select all the documents inside the articles collection where the number of views is less than 20.

db.articles.find({views: {$lt: 20}})

$lte

Matches values that are less than or equal to a specified value.

db.collection.find({field: {$lte: value}})

Example of $lte

Select all the documents inside the articles collection where the number of views is less than or equal 20.

db.articles.find({views: {$lte: 20}})

$in

Matches any of the values specified in an array.

db.collection.find({field: {$in: [value1, value2, ...]}})

Example of $in

Select all the documents inside the articles collection where the category is one the 3 values: food, health or sprot.

db.articles.find({category: {$in: ["food", "health", "sport"]}})

$nin

Matches none of the values specified in an array.

db.collection.find({field: {$nin: [value1, value2, ...]}})

Example of $nin

Select all the documents inside the articles collection where the category is not one the 3 values: food, health or sprot.

db.articles.find({category: {$nin: ["food", "health", "sport"]}})

Logical operators in MongoDB

Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.

$and

Matches none of the values specified in an array.

db.collection.find({
	$and: [
		{field: "value"},
		{field2: {$lte: 30}},
		...
	]
})

Example of $and

Select all the documents inside the articles collection where the number of views is greater than 20 and the author is "yamicode".

db.articles.find({
	$and: [
		{author: "yamicode"},
		{views: {$gt: 20}}
	]
})

Equivalent

db.articles.find({
	author: "yamicode"},
	views: {$gt: 20}
})

$or

Joins query clauses with a logical OR returns all documents that match the conditions of either clause.

db.collection.find({
	$or: [
		{field: "value"},
		{field2: {$lte: 30}},
		...
	]
})

Example of $or

Select all the documents inside the articles collection where the number of views is greater than 20 or the author is "yamicode".

db.articles.find({
	$or: [
		{author: "yamicode"},
		{views: {$gt: 20}}
	]
})

Mixing $and & $or

db.collection.find({
	field: "value",
	$or: [
		{field2: "value"},
		{field3: {$lte: 30}}
	]
})

Example of mixing $and & $or

Select all the documents inside the articles collection where the author is "yamicode" and (the number of views is greater than 20 and it's published.

db.articles.find({
	author: "yamicode",
	$or: [
		{published: true},
		{views: {$lte: 20}}
	]
})

$not

Inverts the effect of a query expression and returns documents that do not match the query expression.

db.collection.find({
	field: 
	{ 
		$not: { $gt: value } 
	}
})

Example of $gt

Select all the documents inside the articles collection where the number of views is less than 20.

db.articles.find({views: {$not: {$gt: 20}}})

$nor

Joins query clauses with a logical NOR returns all documents that fail to match both clauses.

db.collection.find({
	$nor: [
		{field: "value"},
		{field2: {$lte: 30}}
	]
})

Example of $nor

Select all the documents inside the articles collection where the number of views is neither less than 20 or published by "yamicode".

db.articles.find({views: {$not: {$gt: 20}}}
	$nor: [
		{views: {$lt: 20}},
		{author: "yamicode"}
	]
)

Element operators in MongoDB

$exist

Matches documents that have the specified field. you can Find examples in: [$exists]

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

$type

Selects documents if a field is of the specified type. To see more about types visit the following link: [$type]

db.collection.find({ field: {$type: "number" }})

Array operators in MongoDB

$all

Matches arrays that contain all elements specified in the query.

db.collection.find({ arrayField: {$all: [value1, value2]}})

Example of $all

Select all the documents inside the articles collection where the tags array contains the values "mongodb" and "spring". Example: {tags: ["mongodb", "angular", "spring"].

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

Equivalent

db.articles.find({ $and: [ { tags: "spring" }, { tags: "mongodb" } ] })

$elemMatch

Selects documents if element in the array field matches all the specified $elemMatch conditions.

db.collection.find({ field: {$elemMatch: {$gt: 5, $type : "number" }}})

Example of $elemMatch

Select all the documents inside the articles collection where the results array contains one field that is greater than 10 and less than 16 in the same time.

db.articles.find({ results: {$elemMatch: {$gt: 10, $lt: 16}}})

Difference between using $elemMatch and simple array equality

In the examples below the main difference is that the $elemMatch will not return any result because there is not a number that is greater and less than the same number in the same time, but in the second example where we used the equality it's enough to find two values in the array that one of them is less than 16 and the other is greater than 16.

$elemMatch
db.articles.find({ results: {$elemMatch: {$gt: 16, $lt: 16}}})
Array equality
db.articles.find({ results: {$gt: 16, $lt: 16}})

$size

Selects documents if the array field is a specified size.

db.collection.find({ field: {$size: 5 }})