Querying documents in MongoDB is a crucial part of database management. This chapter of the e-book course will cover in detail how to query documents in MongoDB, starting with the basics and moving on to more complex queries.

First, it is important to understand what a query is in the context of MongoDB. A query is basically a question you ask the database. For example, you might want to know which documents in a collection match a certain criteria. The answer to this question is the result of the query.

Basic queries

In MongoDB, the basic query operation is performed using the find() method. This method returns all documents in a collection that match the query. The basic syntax for the find() operation in MongoDB is as follows:

db.collection.find(query, projection)

Where 'query' is a document that specifies the search conditions and 'projection' is a document that specifies the fields to return in the documents that match the query. If the query is not specified, the find() method returns all documents in the collection.

Queries with comparison operators

MongoDB supports several comparison operators such as $eq (equal), $gt (greater than), $gte (greater than or equal), $lt (less than), $lte (less than or equal) and $ne (not equal). These operators can be used to form more complex queries. For example, the following query returns all documents where the 'age' field is greater than 20:

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

Queries with logical operators

Logical operators in MongoDB include $or, $and, $not, and $nor. These operators can be used to combine multiple query conditions. For example, the following query returns all documents where the 'age' field is greater than 20 and the 'name' field is 'John':

db.collection.find({ $and: [ { "age" : { $gt : 20 } }, { "name" : "John" } ] })

Queries with regular expressions

MongoDB also supports regular expression queries, which can be used to search for string patterns. For example, the following query returns all documents where the 'name' field starts with 'J':

db.collection.find({ "name" : /^J/ })

Array queries

MongoDB supports queries on array fields. For example, the following query returns all documents where the 'hobbies' field contains 'soccer':

db.collection.find({ "hobbies" : "football" })

Queries in embedded documents

Documents in MongoDB can contain embedded subdocuments. To query fields in a subdocument, you can use dot notation. For example, the following query returns all documents where the 'endereco.cidade' field is 'São Paulo':

db.collection.find({ "endereco.cidade" : "São Paulo" })

This chapter only covered the basics of queries in MongoDB. However, MongoDB supports many other query features such as full-text queries, geospatial queries, aggregation queries, and more. These features will be covered in detail in subsequent chapters of this eBook course.

Understanding how to query documents in MongoDB is essential to working effectively with this database. With practice, you will be able to formulate complex queries to extract accurate information from your MongoDB database.

Now answer the exercise about the content:

What is the function of the find() method in MongoDB and how is it used?

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

You missed! Try again.

Article image Querying documents in MongoDB: Introduction to querying documents in MongoDB

Next page of the Free Ebook:

27Querying documents in MongoDB: Introduction to querying documents in MongoDB

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou 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