Article image Querying documents in MongoDB: Querying documents using query operators

9.4. Querying documents in MongoDB: Querying documents using query operators

Page 30 | Listen in audio

9.4. Querying Documents in MongoDB: Querying Documents Using Query Operators

MongoDB, a document-oriented NoSQL database, offers a variety of methods for retrieving documents from a collection. Document querying is a crucial aspect of MongoDB, allowing users to access and manipulate stored data. In this chapter, we will explore how to query documents using query operators.

Query operators in MongoDB

Query operators in MongoDB are special symbols that help specify the search condition in a query. They are used with the find() method to retrieve documents that satisfy the search condition.

Basic Query Operators

Some of the basic query operators in MongoDB include:

  • $eq: This operator matches values ​​that are equal to a specified value.
  • $gt: This operator matches values ​​that are greater than a specified value.
  • $gte: This operator matches values ​​that are greater than or equal to a specified value.
  • $lt: This operator matches values ​​that are less than a specified value.
  • $lte: This operator matches values ​​that are less than or equal to a specified value.
  • $ne: This operator matches all values ​​that are not equal to a specified value.
  • $in: This operator matches any value that exists in the specified list.
  • $nin: This operator matches any value that does not exist in the specified list.

Query Examples Using Query Operators

Suppose we have a collection called 'students' with documents that contain information about students, such as name, age and grades. Here are some examples of how you can use query operators to retrieve documents in this collection:

Example 1: Using the $eq Operator

To find a student with the name 'John', you can use the $eq operator as follows:

db.students.find({name: {$eq: 'John'}})

Example 2: Using the $gt Operator

To find students over the age of 20, you can use the $gt operator as follows:

db.students.find({age: {$gt: 20}})

Example 3: Using the $in Operator

To find students who received grades 90, 95 or 100, you can use the $in operator as follows:

db.students.find({grades: {$in: [90, 95, 100]}})

These are just a few examples of how you can use query operators to retrieve documents in MongoDB. In a real-world scenario, you can combine multiple query operators to formulate more complex and powerful queries.

Conclusion

Query operators in MongoDB are powerful tools that allow you to retrieve documents from a collection based on specific search conditions. By understanding how to use these operators, you can access and manipulate your data more efficiently and effectively. Remember that practice is the key to becoming proficient in using query operators, so don't hesitate to try out different operators and operator combinations to become familiar with them.

Now answer the exercise about the content:

What is the role of query operators in MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Querying documents in MongoDB: Querying documents using logical operators

Next page of the Free Ebook:

31Querying documents in MongoDB: Querying documents using logical operators

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text