MongoDB is a popular NoSQL database that stores data in the form of documents instead of rows and columns as in traditional relational databases. In MongoDB, queries and projections are essential tools that enable you to retrieve and manipulate data from the database efficiently. In this article, we’ll explore what MongoDB queries and projections are and show examples of how to use them effectively.
MongoDB Queries
A query in MongoDB is a request for data that meets certain criteria. Queries are used to retrieve data from one or more collections in the database. MongoDB queries are built using a JSON-like syntax called the MongoDB query language. This language is designed to be expressive and easy to use, making it simple for developers to create complex queries.
MongoDB supports a wide range of query operators that enable you to filter, sort, and limit data. Some of the most commonly used operators include:
- $eq: Matches values that are equal to a specified value.
- $ne: Matches values that are not equal to a specified value.
- $gt: Matches values that are greater than a specified value.
- $lt: Matches values that are less than a specified value.
- $gte: Matches values that are greater than or equal to a specified value.
- $lte: Matches values that are less than or equal to a specified value.
- $in: Matches any of the values specified in an array.
- $nin: Matches none of the values specified in an array.
- $and: Joins query clauses with a logical AND.
- $or: Joins query clauses with a logical OR.
Let’s take a look at an example of a MongoDB query. Suppose we have a collection called “employees” with documents that have fields for “name”, “department”, and “salary”. We can use the following query to retrieve all employees who work in the “marketing” department and earn more than $50,000 per year:
db.employees.find({ department: "marketing", salary: { $gt: 50000 } })
In this query, we use the find()
method to retrieve all documents that match the specified criteria. The criteria are passed as a JSON object, where the “department” field is set to “marketing” and the “salary” field uses the $gt
operator to match values greater than 50,000.
MongoDB Projections
Projections in MongoDB are used to specify which fields should be returned in the query result. By default, MongoDB returns all fields in a document. However, in some cases, you may only need to retrieve a subset of fields to reduce network overhead or improve performance.
Projections are defined as a second argument to the find()
method and use a similar JSON syntax as queries. Projections use a boolean value to indicate whether a field should be included or excluded from the result. If a field is included, the value is set to 1
. If a field is excluded, the value is set to 0
.
Let’s take a look at an example. Suppose we want to retrieve only the names and salaries of employees who work in the “marketing” department and earn more than $50,000 per year. We can use the following query with a projection
db.employees.find({ department: "marketing", salary: { $gt: 50000 } }, { name: 1, salary: 1, _id: 0 })
In this query, we pass a second argument to the find()
method that specifies the fields to include or exclude from the result. We include the “name” and “salary” fields by setting their values to 1
. We exclude the “_id” field by setting its value to 0
.