0
0
MongoDBquery~30 mins

$eq for equality in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
$eq for equality in MongoDB queries
📖 Scenario: You are managing a small library database. You want to find books by a specific author.
🎯 Goal: Build a MongoDB query using the $eq operator to find all books where the author matches a given name.
📋 What You'll Learn
Create a collection named books with sample book documents
Define a variable authorName with the exact author name to search
Write a MongoDB query using $eq to find books by authorName
Complete the query with a projection to show only the title and author fields
💡 Why This Matters
🌍 Real World
Finding specific records in a database by matching exact values is common in apps like libraries, stores, and user management systems.
💼 Career
Knowing how to use <code>$eq</code> helps you filter data efficiently in MongoDB, a popular NoSQL database used in many companies.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a variable called books that is a list of three documents with these exact entries: { title: "The Hobbit", author: "J.R.R. Tolkien" }, { title: "1984", author: "George Orwell" }, and { title: "Animal Farm", author: "George Orwell" }.
MongoDB
Need a hint?

Use a list of dictionaries to represent the collection.

2
Define the author name to search
Create a variable called authorName and set it to the string "George Orwell".
MongoDB
Need a hint?

Set the variable exactly as shown.

3
Write the MongoDB query using $eq
Create a variable called query and set it to a dictionary that uses $eq to match the author field to the variable authorName. The query should look like { "author": { "$eq": authorName } }.
MongoDB
Need a hint?

Use the $eq operator inside the query dictionary.

4
Add the projection to show only title and author
Create a variable called projection and set it to a dictionary that includes "title": 1 and "author": 1 to show only these fields in the query result.
MongoDB
Need a hint?

Use 1 to include fields in the projection.