Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
MongoDB Fundamentals

You're reading from  MongoDB Fundamentals

Product type Book
Published in Dec 2020
Publisher Packt
ISBN-13 9781839210648
Pages 748 pages
Edition 1st Edition
Languages
Concepts
Authors (4):
Amit Phaltankar Amit Phaltankar
Profile icon Amit Phaltankar
Juned Ahsan Juned Ahsan
Profile icon Juned Ahsan
Michael Harrison Michael Harrison
Profile icon Michael Harrison
Liviu Nedov Liviu Nedov
Profile icon Liviu Nedov
View More author details

Table of Contents (15) Chapters

Preface
1. Introduction to MongoDB 2. Documents and Data Types 3. Servers and Clients 4. Querying Documents 5. Inserting, Updating, and Deleting Documents 6. Updating with Aggregation Pipelines and Arrays 7. Data Aggregation 8. Coding JavaScript in MongoDB 9. Performance 10. Replication 11. Backup and Restore in MongoDB 12. Data Visualization 13. MongoDB Case Study Appendix

4. Querying Documents

Overview

This chapter discusses how to prepare and execute queries in MongoDB. You will learn how to find documents from a collection and limit the fields shown in the output. You will use various conditional and logical operators, as well as combinations of them, in a query and use regular expressions to find documents in a collection. By the end of this chapter, you will be able to run queries on arrays and nested objects, as well as limit, skip, and sort the records in the result set.

Introduction

In the previous chapters, we covered the basics of MongoDB, its document-based data model, data types, clients, and the MongoDB server. We created an Atlas cluster on the cloud, loaded sample datasets, and connected using different clients. Now that we have the data, we can start writing queries to retrieve documents from the collections. Queries are used to retrieve meaningful data from the database. We will begin by learning about query syntax, how to use operators, and the techniques we can use to format the result sets. Practicing and mastering the query language will help you find any required document quickly and efficiently.

For any database management system, having a powerful query language is as important as its storage model, or its scalability. Consider that you are working on a database platform that offers an excellent storage model or an extremely high-performance database engine. However, it has very poor query language support, because of which you...

MongoDB Query Structure

MongoDB queries are based on JSON documents in which you write your criteria in the form of valid documents. With the data stored in the form of JSON-like documents, the queries seem more natural and readable. The following diagram is an example of a simple MongoDB query that finds all the documents where the name field contains the value David:

Figure 4.1: MongoDB Query Syntax

To draw a comparison with SQL, let's rewrite the same query in SQL format. This query finds all the rows from the USERS table that contain the name column where the value of name is David, as follows:

SELECT * FROM USERS WHERE name = 'David';

The most notable difference between the preceding queries is that the MongoDB queries do not have keywords such as SELECT, FROM, and WHERE. Thus, you need not remember a lot of keywords and their uses.

The absence of keywords makes the queries less wordy and hence more focused, and less error-prone...

Basic MongoDB Queries

All the queries in this section are top-level queries; that is, they are based on the top-level (also known as root-level) fields in the documents. We will learn about the basic query operators by writing queries against the root fields.

Finding Documents

The most basic query in MongoDB is performed with the find() function on the collection. When this function is executed without any argument, it returns all the documents in a collection. For example, consider the following query:

db.comments.find()

This query calls the find() function on the collection named comments. When executed on a mongo shell, it will return all the documents from the collection. To return only specific documents, a condition can be provided to the find() function. When this is done, the find() function evaluates it against each and every document in the collection and returns the documents that match the condition.

For example, consider that instead of retrieving all the...

Conditional Operators

Now that you have learned how to query MongoDB collections, as well as how to use projection to return only specific fields in the output, it is time to learn more advanced ways of querying. So far, you've tried to query the comments collection using the value of a field. However, there are more ways to query documents. MongoDB provides conditional operators that can be used to represent various conditions, such as equality, and whether a value is less than or greater than some specified value. In this section, we will explore these operators and learn how to use them in queries.

Equals ($eq)

In the preceding section, you saw examples of equality checking where the queries used a key-value pair. However, queries can also use a dedicated operator ($eq) to find documents with fields that match a given value. For example, the following queries find and return movies that have exactly 5 comments. Both queries have the same effect:

db.movies.find({"...

Logical Operators

So far, we have learned about various operators used for writing comparison-based queries. The queries we have written so far had only one criterion at a time. But in practical scenarios, you may need to write more complex queries. MongoDB provides four logical operators to help you build logical combinations of multiple criteria in the same query. Let's have a look at them.

$and operator

Using the $and operator, you can have any number of conditions wrapped in an array and the operator will return only the documents that satisfy all the conditions. When a document fails a condition check, the next conditions are skipped. That is why the operator is called a short-circuit operator. For example, say you want to determine the count of unrated movies that were released in 2008. This query must have two conditions:

  • The field rated should have a value of UNRATED
  • The field year must be equal to 2008

In the document format, both queries can...

Regular Expressions

In a real-world movie service, you will want to provide auto-completion search boxes where, as soon as the user types in a few characters of the movie title, the search box suggests all the movies whose titles match the character sequence typed in. This is implemented using regular expressions. A regular expression is a special string that defines a character pattern. When such a regular expression is used to find string fields, all the strings that have the matching pattern are found and returned.

In MongoDB queries, regular expressions can be used with the $regex operator. Imagine you have typed the word Opera into the search box and want to find all the movies whose titles contain this character pattern. The regular expression query for this will be as follows:

db.movies.find(
    {"title" : {$regex :"Opera"}}
)

Upon executing this query and using projection to print only the titles, the result will appear as follows...

Query Arrays and Nested Documents

In Chapter 2, Documents and Data Types, we learned that MongoDB documents support complex object structures such as arrays, nested objects, arrays of objects, and more. The arrays and nested documents help store self-contained information. It is extremely important to have a mechanism to easily search for and retrieve the information stored in such complex structures. The MongoDB query language allows us to query such complex structures in the most intuitive manner. First, we will learn how to run queries on the array elements, and then we will learn how to run them on nested object fields.

Finding an Array by an Element

Querying over an array is similar to querying any other field. In the movies collection, there are several arrays, and the cast field is one of them. Consider that, in your movies service, the user wants to find movies starring the actor Charles Chaplin. To create the query for this search, use an equality check on the field...

Limiting, Skipping, and Sorting Documents

So far, we have learned how to write basic and complex queries and to project fields in the resulting documents. In this section, you will learn how to control the number and order of documents returned by a query.

Let's talk about why the amount of data a query returns needs to be controlled. In most real-world cases, you won't be using all the documents your query matches to. Imagine that a user of our movie service is planning to watch a drama movie tonight. They will visit the movie store and search for drama movies and find that there are more than 13,000 of these in the collection. With such a large search result, they might spend the entire night just looking through the various movies and deciding which one to watch.

For a better user experience, you may want to show the 10 most popular movies in the drama category at a time, followed by the next 10 in the sequence, and so on. This technique of serving data is known...

Summary

We started this chapter with a detailed study of the structure of MongoDB queries and how different they are from SQL queries. Then, we implemented these queries to find and count the documents and limit the number of fields returned in the result using various examples. We also learned about the various conditional and logical operators and practiced using them in combination to notice the difference in results.

We then learned how to provide a text pattern using regular expressions to filter our search results, and covered how to query arrays and nested objects and include their specific fields in the results. Finally, we learned how to paginate large result sets by using limiting, sorting, and skipping on the documents in the result.

In the next chapter, we will learn how to insert, update, and delete documents from MongoDB collections.

lock icon The rest of the chapter is locked
You have been reading a chapter from
MongoDB Fundamentals
Published in: Dec 2020 Publisher: Packt ISBN-13: 9781839210648
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}