0
0
MongoDBquery~5 mins

$nin for not in set in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the $nin operator do in MongoDB?

The $nin operator selects documents where the value of a field is not in the specified array of values.

Click to reveal answer
beginner
How would you find documents where the field color is not 'red', 'blue', or 'green' using $nin?
{ color: { $nin: ['red', 'blue', 'green'] } }

This query returns documents where color is any value except 'red', 'blue', or 'green'.

Click to reveal answer
intermediate
Can $nin be used with fields that contain arrays?

Yes, $nin checks if none of the values in the field's array are in the specified array. If any value matches, the document is excluded.

Click to reveal answer
intermediate
What happens if you use $nin with an empty array like { field: { $nin: [] } }?

The query matches all documents because no values are excluded.

Click to reveal answer
beginner
Is $nin the opposite of $in?

Yes, $nin selects documents where the field's value is not in the given array, while $in selects documents where the field's value is in the array.

Click to reveal answer
Which MongoDB operator selects documents where a field's value is NOT in a list of values?
A$nin
B$in
C$eq
D$ne
What will the query { age: { $nin: [20, 30, 40] } } return?
ADocuments where age is 20, 30, or 40
BDocuments where age is not 20, 30, or 40
CDocuments where age is exactly 20
DDocuments where age is missing
If a field contains an array, how does $nin behave?
AMatches if any element is in the array
BMatches if all elements are in the array
CMatches if none of the elements are in the array
DDoes not work with arrays
What does { field: { $nin: [] } } match?
ANo documents
BAll documents
CDocuments where field is null
DDocuments where field is an empty array
Which operator is the direct opposite of $nin?
A$ne
B$in
C$eq
D$exists
Explain how the $nin operator works in MongoDB queries.
Think about how you exclude certain items from a list.
You got /3 concepts.
    Describe a real-life example where you might use $nin in a database query.
    Imagine you want to find all fruits except apples and bananas.
    You got /3 concepts.