0
0
MongoDBquery~30 mins

Query filter syntax in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Query Filter Syntax in MongoDB
📖 Scenario: You are managing a small online bookstore database. You want to find books based on certain criteria like author, price, and availability.
🎯 Goal: Build MongoDB queries step-by-step to filter books by author, price, and stock status using query filter syntax.
📋 What You'll Learn
Create a collection named books with sample book documents
Add a filter variable to specify query conditions
Write a MongoDB query using the filter to find matching books
Complete the query with a projection to show only title and author
💡 Why This Matters
🌍 Real World
Filtering data is a common task in databases to find specific records quickly, such as finding books by a certain author or price range.
💼 Career
Understanding query filter syntax is essential for database administrators, backend developers, and data analysts who work with MongoDB or similar NoSQL databases.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it a list of three book documents with these exact fields and values: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10, inStock: true }, { title: "1984", author: "George Orwell", price: 15, inStock: false }, and { title: "To Kill a Mockingbird", author: "Harper Lee", price: 12, inStock: true }.
MongoDB
Need a hint?

Use a list of dictionaries to represent the books collection with exact field names and values.

2
Create a filter to find books by author "George Orwell"
Create a variable called filter and assign it a dictionary with the key author and value "George Orwell" to filter books by this author.
MongoDB
Need a hint?

Use a dictionary with the key author and value "George Orwell" to create the filter.

3
Write a MongoDB query using the filter to find matching books
Create a variable called query_result and assign it the result of filtering the books list using the filter variable. Use a list comprehension that checks if each book's author matches the filter's author.
MongoDB
Need a hint?

Use a list comprehension to select books where the author matches the filter's author.

4
Complete the query with a projection to show only title and author
Create a variable called final_result and assign it a list of dictionaries containing only the title and author fields from each book in query_result. Use a list comprehension to extract these fields.
MongoDB
Need a hint?

Use a list comprehension to create a new list with dictionaries containing only title and author from each book.