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
$nin for not in set in MongoDB
📖 Scenario: You are managing a small online bookstore database. You want to find books that are not in certain genres to recommend to customers who want something different.
🎯 Goal: Build a MongoDB query using the $nin operator to find books whose genre is not in a given list of genres.
📋 What You'll Learn
Create a collection called books with specific book documents
Define a list of genres to exclude
Write a query using $nin to find books not in those genres
Complete the query to return only the book titles
💡 Why This Matters
🌍 Real World
Filtering data to exclude certain categories is common in recommendation systems, reporting, and data analysis.
💼 Career
Understanding $nin helps in writing flexible MongoDB queries for real-world applications like filtering products, users, or content.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a MongoDB collection called books and insert these exact documents: { title: "The Hobbit", genre: "Fantasy" }, { title: "1984", genre: "Dystopian" }, { title: "To Kill a Mockingbird", genre: "Classic" }, { title: "The Great Gatsby", genre: "Classic" }, { title: "Neuromancer", genre: "Science Fiction" }.
MongoDB
Hint
Use db.books.insertMany([...]) with the exact documents listed.
2
Define the list of genres to exclude
Create a variable called excludedGenres and set it to an array containing the strings "Classic" and "Dystopian".
MongoDB
Hint
Use const excludedGenres = ["Classic", "Dystopian"] to define the array.
3
Write the query using $nin to find books not in excluded genres
Write a MongoDB query called query that finds documents in books where the genre field is $nin the excludedGenres array.
MongoDB
Hint
Use { genre: { $nin: excludedGenres } } to build the query object.
4
Complete the query to return only book titles
Write a MongoDB find command using query to find matching books in books and project only the title field (exclude _id). Save this to a variable called result.
MongoDB
Hint
Use db.books.find(query, { title: 1, _id: 0 }) to get only titles.
Practice
(1/5)
1. What does the MongoDB operator $nin do in a query?
easy
A. Selects documents where the field's value is in the specified array
B. Selects documents where the field's value is NOT in the specified array
C. Updates documents with values in the specified array
D. Deletes documents where the field's value is in the specified array
Solution
Step 1: Understand the purpose of $nin
The $nin operator is used to filter documents where a field's value is NOT included in a given list of values.
Step 2: Compare with other operators
Unlike $in which selects values inside the array, $nin excludes those values.
Final Answer:
Selects documents where the field's value is NOT in the specified array -> Option B
Quick Check:
$nin excludes values = B [OK]
Hint: Remember: $nin means NOT in list [OK]
Common Mistakes:
Confusing $nin with $in
Thinking it updates or deletes documents
Using it to select values inside the array
2. Which of the following is the correct syntax to find documents where the field status is NOT 'active' or 'pending' using $nin?
easy
A. { status: { $nin: ['active', 'pending'] } }
B. { status: { $nin: 'active', 'pending' } }
C. { status: { $nin: ['active'] || ['pending'] } }
D. { status: { $nin: 'active' && 'pending' } }
Solution
Step 1: Check the correct $nin syntax
The $nin operator requires an array of values inside square brackets to specify the excluded set.
Step 2: Validate each option
{ status: { $nin: ['active', 'pending'] } } correctly uses an array with two strings. Options B, C, and D have syntax errors or invalid expressions.