0
0
MongoDBquery~30 mins

How MongoDB scans documents - Try It Yourself

Choose your learning style9 modes available
How MongoDB scans documents
📖 Scenario: You are working with a MongoDB collection that stores information about books in a library. You want to understand how MongoDB scans documents when you query the collection.
🎯 Goal: Build a simple MongoDB collection with book documents, add a filter condition, and perform a query to see how MongoDB scans documents.
📋 What You'll Learn
Create a MongoDB collection named library with three book documents
Add a filter variable called filter to select books published after 2010
Write a query using find() with the filter to scan documents
Add a projection to return only the title and author fields
💡 Why This Matters
🌍 Real World
MongoDB is widely used to store and query document data in web applications, content management, and analytics.
💼 Career
Understanding how MongoDB scans documents helps optimize queries and improve application performance in real-world projects.
Progress0 / 4 steps
1
Create the library collection with book documents
Create a MongoDB collection called library and insert these three documents exactly: { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', year: 1925 }, { title: 'The Road', author: 'Cormac McCarthy', year: 2006 }, { title: 'The Martian', author: 'Andy Weir', year: 2014 }
MongoDB
Need a hint?

Use db.library.insertMany() with an array of three book objects.

2
Add a filter to select books published after 2010
Create a variable called filter that selects documents where the year field is greater than 2010
MongoDB
Need a hint?

Use { year: { $gt: 2010 } } as the filter object.

3
Query the library collection using the filter
Write a query using db.library.find(filter) to scan documents matching the filter
MongoDB
Need a hint?

Use db.library.find(filter) to get documents matching the filter.

4
Add a projection to return only title and author
Modify the query to include a projection that returns only the title and author fields by using db.library.find(filter, { title: 1, author: 1, _id: 0 })
MongoDB
Need a hint?

Use the second argument in find() to specify the projection fields.