MongoDB CRUD Operations: Modifying Query Results
Learn how to sort and limit query results and how to count documents in MongoDB.
MongoDB CRUD Operations: Modifying Query Results
Unit Overview
In this unit, you’ll learn how to modify query results in MongoDB by using sorts, limits, projections, and counts. First, you will learn how to organize query results by sorting and limiting the documents that are returned. Then you'll explore how to use projection to return selected fields from a query. Finally, you’ll learn how to count the number of documents that match a query. Using these query modifications will help enhance the functionality and performance of your applications.
Prerequisites
- Getting Started with MongoDB Atlas
- MongoDB and the Document Model
- MongoDB CRUD Operations: Insert and Find
- MongoDB CRUD Operations: Replace and Delete
Lessons in This Unit
- Lesson 01: Sorting and Limiting Query Results in MongoDB
- Lesson 02: Returning Specific Data from a Query in MongoDB
- Lesson 03: Counting Documents in a MongoDB Collection
Camden Kirkland I Technical Instructor
Cam is a Technical Instructor at MongoDB where he delivers courses for a wide variety of foundational and advanced topics on the database. Prior to MongoDB, he was a Curriculum Architect designing technical programs for web development, data analytics, and financial technologies for universities all over the country. In a past life he was a Chemist making sure your transformers didn't catch fire.
Emily Pope | Senior Curriculum Designer
Emily is a Senior Curriculum Designer at MongoDB. Prior to MongoDB, Emily worked closely with professors at MIT and Columbia to design bootcamps in full stack development and data science at Emeritus. She also worked as an instructional designer on database and computer science learning experiences at Cengage. Emily loves learning about the everchanging tech space and is passionate about making these skills accessible to a global audience.
Hello and welcome!
My name is Camden. I’m a Curriculum Engineer at MongoDB, and I’ll be working with you through this unit.
In this unit, you’lll learn how to modify query results in MongoDB by, using sorts, limits, projections, and counts.
Knowing how to modify query results is a fundamental skill when working with any database.
Often when we’re writing queries, we want to specify how results are returned.
We might want to sort the results, or limit the number of documents returned. We might want to return only selected fields, or just count the number of documents matching a query without returning them.
Being able to make these query modifications will help enhance the functionality and performance of your applications.
This unit consists of three lessons. Each lesson begins with a short video explanation and demo, followed by one to three lab activities.
In the first lesson, we’ll learn how to organize query results by sorting and limiting the documents returned.
Next, we’ll learn to return selected fields from a query using projection.
Finally, we’ll learn to count the number of documents matching a query.
Before beginning this unit, you should know the basics of querying MongoDB documents using the `db.collection.find()` method.
If you’re new to querying with MongoDB, I recommend first checking out the unit on inserting and finding documents, and then returning to this unit.
By the end of the unit, you’ll be sorting, limiting, projecting and counting your query results like a pro.
Let’s get started!