Use $nin to find items that do NOT match any value in a list. It helps exclude unwanted data.
$nin for not in set in MongoDB
Introduction
Syntax
MongoDB
{ field: { $nin: [value1, value2, ...] } }The $nin operator takes an array of values to exclude.
It matches documents where the field's value is not in the given array.
Examples
category is NOT 'electronics' or 'furniture'.MongoDB
{ category: { $nin: ['electronics', 'furniture'] } }age is not 20, 30, or 40.MongoDB
{ age: { $nin: [20, 30, 40] } }status is neither 'pending' nor 'cancelled'.MongoDB
{ status: { $nin: ['pending', 'cancelled'] } }Sample Program
This query finds all orders where the status is NOT 'shipped' or 'delivered'.
MongoDB
db.orders.find({ status: { $nin: ['shipped', 'delivered'] } })Important Notes
If the field does not exist in a document, that document will be included because its value is not in the list.
$nin is the opposite of $in, which finds values inside the list.
Summary
$nin helps exclude documents with certain values.
Use it when you want to filter out unwanted data from your results.
It works by matching documents where the field's value is NOT in the given array.
Practice
1. What does the MongoDB operator
$nin do in a query?easy
Solution
Step 1: Understand the purpose of
The$nin$ninoperator 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$inwhich selects values inside the array,$ninexcludes those values.Final Answer:
Selects documents where the field's value is NOT in the specified array -> Option BQuick Check:
$ninexcludes values = B [OK]
Hint: Remember:
$nin means NOT in list [OK]Common Mistakes:
- Confusing
$ninwith$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
Solution
Step 1: Check the correct
The$ninsyntax$ninoperator 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.Final Answer:
{ status: { $nin: ['active', 'pending'] } } -> Option AQuick Check:
Correct array syntax for$nin= A [OK]
Hint: Use square brackets for arrays in
$nin [OK]Common Mistakes:
- Using multiple arguments instead of an array
- Using logical operators inside
$nin - Missing square brackets around values
3. Given the collection
What will be the result of this query?
products with documents:{ name: 'Pen', category: 'stationery' }{ name: 'Apple', category: 'fruit' }{ name: 'Notebook', category: 'stationery' }{ name: 'Carrot', category: 'vegetable' }What will be the result of this query?
db.products.find({ category: { $nin: ['fruit', 'vegetable'] } })medium
Solution
Step 1: Understand the
The query excludes documents where$ninfiltercategoryis 'fruit' or 'vegetable'.Step 2: Identify matching documents
Documents with category 'stationery' are not in ['fruit', 'vegetable'], so they match. 'Pen' and 'Notebook' have 'stationery'.Final Answer:
[{ name: 'Pen', category: 'stationery' }, { name: 'Notebook', category: 'stationery' }] -> Option DQuick Check:
Exclude 'fruit' and 'vegetable' = stationery items only [OK]
Hint: Exclude unwanted categories with
$nin array [OK]Common Mistakes:
- Confusing
$ninwith$in - Expecting documents with excluded categories
- Assuming empty result when some match
4. You wrote this query to exclude users with roles 'admin' or 'moderator':
But it throws an error. What is the problem?
db.users.find({ role: { $nin: 'admin', 'moderator' } })But it throws an error. What is the problem?
medium
Solution
Step 1: Check
$ninsyntax$ninexpects a single array argument listing values to exclude.Step 2: Identify the error in the query
The query passes two separate string arguments instead of one array, causing syntax error.Final Answer:
The$ninoperator requires an array, not multiple arguments -> Option AQuick Check:
$ninneeds array input = C [OK]
Hint: Always wrap values in an array for
$nin [OK]Common Mistakes:
- Passing multiple arguments instead of an array
- Using wrong operator
$inby mistake - Assuming
$ninis unsupported
5. You have a collection
You want to find orders NOT in statuses 'pending' or 'cancelled' AND exclude orders with
orders with documents:{ orderId: 1, status: 'shipped' }{ orderId: 2, status: 'pending' }{ orderId: 3, status: 'cancelled' }{ orderId: 4, status: 'delivered' }You want to find orders NOT in statuses 'pending' or 'cancelled' AND exclude orders with
orderId 4. Which query correctly uses $nin to achieve this?hard
Solution
Step 1: Exclude statuses 'pending' and 'cancelled'
Use{ status: { $nin: ['pending', 'cancelled'] } }to exclude these statuses.Step 2: Exclude orderId 4 using
Use$nin{ orderId: { $nin: [4] } }to exclude orderId 4 as well.Step 3: Combine conditions
Both conditions together filter out unwanted statuses and orderId 4.Final Answer:
{ status: { $nin: ['pending', 'cancelled'] }, orderId: { $nin: [4] } } -> Option CQuick Check:
Use$ninon both fields = A [OK]
Hint: Use separate
$nin for each field to exclude multiple sets [OK]Common Mistakes:
- Putting orderId inside status array
- Using
$neinstead of$ninfor multiple values - Using
$into exclude values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
