0
0
MongoDBquery~30 mins

$or operator behavior in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
$or Operator Behavior in MongoDB Queries
📋 What You'll Learn
💡 Why This Matters
🌍 Real World
Using the $or operator is common when searching databases for records that meet one or more criteria, such as finding products on sale or customers from specific cities.
💼 Career
Understanding how to use $or in MongoDB queries is essential for database developers and data analysts to retrieve flexible and accurate data from NoSQL databases.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array with these exact documents: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10 }, { title: "1984", author: "George Orwell", price: 15 }, { title: "To Kill a Mockingbird", author: "Harper Lee", price: 8 }, { title: "The Catcher in the Rye", author: "J.D. Salinger", price: 12 }.
MongoDB
Need a hint?

Use an array of objects with the exact titles, authors, and prices given.

2
Define the price limit variable
Create a variable called priceLimit and set it to 12.
MongoDB
Need a hint?

Use const priceLimit = 12; to set the price limit.

3
Write the MongoDB query using the $or operator
Create a variable called query and assign it an object with a $or array containing two conditions: { author: "George Orwell" } and { price: { $lt: priceLimit } }.
MongoDB
Need a hint?

Use $or with an array of two condition objects inside the query object.

4
Complete the query by finding matching books
Create a variable called matchingBooks and assign it the result of filtering books using the query conditions with $or logic.
MongoDB
Need a hint?

Use Array.filter and Array.some to apply the $or conditions.