Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
$nor Operator Behavior in MongoDB Queries
📖 Scenario: You are managing a small online bookstore database. You want to find books that do not match certain conditions, such as not being in a specific genre and not having a rating above a certain value.
🎯 Goal: Build a MongoDB query using the $nor operator to find books that do not meet either of two conditions.
📋 What You'll Learn
Create a collection called books with 4 documents having fields title, genre, and rating.
Create a variable called queryConditions that holds an array of two conditions: one for genre equal to Fantasy, and one for rating greater than 4.5.
Write a MongoDB query using $nor with queryConditions to find books that are neither Fantasy genre nor have a rating above 4.5.
Assign the final query object to a variable called finalQuery.
💡 Why This Matters
🌍 Real World
Filtering data in MongoDB to exclude certain categories or values is common in applications like online stores, libraries, or user management systems.
💼 Career
Understanding $nor helps you write complex queries to retrieve exactly the data you need, a valuable skill for backend developers and data analysts working with MongoDB.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array with these exact 4 documents: { title: 'The Hobbit', genre: 'Fantasy', rating: 4.8 }, { title: '1984', genre: 'Dystopian', rating: 4.6 }, { title: 'Clean Code', genre: 'Programming', rating: 4.7 }, and { title: 'Cooking 101', genre: 'Cooking', rating: 4.2 }.
MongoDB
Hint
Use an array of objects with the exact field names and values.
2
Create the queryConditions array with two conditions
Create a variable called queryConditions and assign it an array with two objects: one with { genre: 'Fantasy' } and the other with { rating: { $gt: 4.5 } }.
MongoDB
Hint
Use an array with two objects exactly as shown.
3
Write the MongoDB query using $nor with queryConditions
Create a variable called finalQuery and assign it an object with the $nor operator using the queryConditions array.
MongoDB
Hint
Use an object with $nor as the key and queryConditions as the value.
4
Complete the query setup for use in MongoDB
Add a comment above finalQuery explaining that this query finds books that are neither Fantasy genre nor have a rating above 4.5.
MongoDB
Hint
Write a clear comment above the finalQuery variable.
Practice
(1/5)
1. What does the $nor operator do in MongoDB queries?
easy
A. Finds documents where all specified conditions are true
B. Finds documents where at least one condition is true
C. Finds documents where none of the specified conditions are true
D. Finds documents that match exactly one condition
Solution
Step 1: Understand the purpose of $nor
The $nor operator returns documents that do not satisfy any of the given conditions.
Step 2: Compare with other logical operators
Unlike $and or $or, $nor excludes documents matching any condition in its array.
Final Answer:
Finds documents where none of the specified conditions are true -> Option C
Quick Check:
$nor excludes all matching conditions = B [OK]
Hint: Think: no conditions should be true for $nor [OK]
Common Mistakes:
Confusing $nor with $or
Assuming it returns documents matching any condition
Thinking it requires all conditions to be true
2. Which of the following is the correct syntax to use $nor in a MongoDB query to exclude documents where age is 25 or status is "active"?
$nor requires an array of condition objects inside square brackets.
Step 2: Check each option's structure
{ $nor: [ { age: 25 }, { status: "active" } ] } correctly uses an array of conditions. Options B and D use objects incorrectly, and C has invalid array syntax.