0
0
MongoDBquery~10 mins

$nin for not in set in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $nin for not in set
Start Query
Check each document's field
Is field value NOT in $nin array?
NoExclude document
Yes
Include document in result
Repeat for all documents
Return filtered documents
The query checks each document's field value and includes it only if the value is NOT in the given $nin array.
Execution Sample
MongoDB
db.collection.find({ age: { $nin: [20, 30, 40] } })
Finds documents where the age is NOT 20, 30, or 40.
Execution Table
StepDocumentField Value (age)Check $nin ConditionInclude in Result?
1{_id:1, age:25}2525 NOT in [20,30,40] -> TrueYes
2{_id:2, age:30}3030 NOT in [20,30,40] -> FalseNo
3{_id:3, age:35}3535 NOT in [20,30,40] -> TrueYes
4{_id:4, age:40}4040 NOT in [20,30,40] -> FalseNo
5{_id:5, age:50}5050 NOT in [20,30,40] -> TrueYes
6All documents checked--Query ends
💡 All documents checked; only those with age NOT in [20,30,40] included.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Current DocumentNone{_id:1, age:25}{_id:2, age:30}{_id:3, age:35}{_id:4, age:40}{_id:5, age:50}None
Included in ResultNoYesNoYesNoYesYes documents: {_id:1,3,5}
Key Moments - 2 Insights
Why is the document with age 30 excluded even though 30 is a number?
Because $nin means 'NOT in' the given array. Since 30 is in [20,30,40], it fails the condition and is excluded (see execution_table step 2).
What happens if the field is missing in a document?
Documents missing the field are included because their field value is considered undefined, which is not in the $nin array (not shown in this example but important to know).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which document is included at step 3?
A{_id:4, age:40}
B{_id:3, age:35}
C{_id:2, age:30}
D{_id:5, age:50}
💡 Hint
Check the 'Include in Result?' column at step 3 in the execution_table.
At which step does the condition become false?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Look for 'False' in the 'Check $nin Condition' column in the execution_table.
If we add 50 to the $nin array, what happens to the document at step 5?
AIt is excluded from the result
BIt is included in the result
CIt causes an error
DIt is included only if age is 50
💡 Hint
Refer to how $nin excludes values present in its array, as shown in the execution_table.
Concept Snapshot
$nin operator filters documents where the field's value is NOT in the specified array.
Syntax: { field: { $nin: [value1, value2, ...] } }
Includes documents with field values outside the array.
Missing fields are included by default.
Useful to exclude multiple values in one query.
Full Transcript
This visual execution shows how MongoDB's $nin operator works. The query checks each document's field value against an array of excluded values. If the value is NOT in the array, the document is included in the result. For example, with $nin: [20,30,40], documents with age 25, 35, and 50 are included, while those with 30 and 40 are excluded. The execution table traces each document step-by-step, showing the condition check and inclusion decision. Key points include understanding that $nin excludes values inside the array and includes missing fields. The quiz reinforces these ideas by asking about specific steps and changes to the $nin array.