Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

MongoDB Query Operators

1. Introduction

MongoDB provides a rich set of query operators that allow you to perform complex queries on your data. These operators can be used to filter documents, compare values, and perform logical operations. Understanding these operators is essential for effective data retrieval in MongoDB.

2. Query Operators

Query operators in MongoDB can be categorized into several groups based on their functionality. Below are the main categories:

2.1 Comparison Operators

Comparison operators allow you to compare values in queries. Common comparison operators include:

  • $eq: Matches values that are equal to a specified value.
  • $gt: Matches values that are greater than a specified value.
  • $gte: Matches values that are greater than or equal to a specified value.
  • $lt: Matches values that are less than a specified value.
  • $lte: Matches values that are less than or equal to a specified value.
  • $ne: Matches values that are not equal to a specified value.

Example of Comparison Operators


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

This query retrieves documents from the collection where the age is greater than or equal to 18.

2.2 Logical Operators

Logical operators are used to combine multiple query conditions. Key logical operators include:

  • $and: Joins query clauses with a logical AND.
  • $or: Joins query clauses with a logical OR.
  • $not: Inverts the effect of a query condition.
  • $nor: Joins query clauses with a logical NOR.

Example of Logical Operators


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

This query retrieves documents where age is either less than 18 or greater than 65.

2.3 Element Operators

Element operators are used to query for the existence of fields or check their types. The most commonly used element operators include:

  • $exists: Checks for the existence of a field.
  • $type: Checks the BSON type of a field.

Example of Element Operators


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

This query retrieves documents where the specified field exists.

2.4 Array Operators

Array operators are used to query arrays within documents. Key array operators include:

  • $all: Matches arrays that contain all specified elements.
  • $elemMatch: Matches documents that contain an array field with at least one element that meets the specified criteria.
  • $size: Matches arrays with a specified number of elements.

Example of Array Operators


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

This query retrieves documents where the tags array contains both "mongodb" and "database".

3. FAQ

What is a query operator in MongoDB?

A query operator is a special symbol or keyword that specifies the criteria for querying documents in a MongoDB collection.

How do I perform a query using multiple operators?

You can combine multiple query operators using logical operators like $and and $or to create complex queries.

Are query operators case-sensitive?

Yes, query operators in MongoDB are case-sensitive, so they must be written in the correct case.