Introduction
MongoDB $in and $nin operators are useful when you need to match a field against multiple possible values. $in returns documents where a field matches at least one value from a given list, while $nin returns documents that do not match any value from the list. In this chapter, you will practice both operators with simple examples involving courses, ages, cities, skills, and categories. MongoDB $in and $nin Practice Questions with Solutions to help you understand the concepts.
Q1. Find Students Studying Python or Java
Problem Statement
Find all students whose course is either Python or Java.
MongoDB Command / Query
db.students.find({
course: { $in: ["Python", "Java"] }
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Aman",
age: 18,
course: "Java"
}
]
Explanation
$in checks whether the field value matches at least one value in the specified array.
Here:
course = Python
OR
course = Java
Q2. Find Students Who Are Not Studying Python or Java
Problem Statement
Find students whose course is neither Python nor Java.
MongoDB Command / Query
db.students.find({
course: { $nin: ["Python", "Java"] }
})
Expected Output
[
{
name: "Priya",
age: 15,
course: "JavaScript"
},
{
name: "Neha",
age: 17,
course: "C++"
}
]
Explanation
$nin means not in.
The query excludes students studying Python or Java.
Q3. Find Students From Selected Cities
Problem Statement
Find students who live in either Delhi, Mumbai, or Pune.
MongoDB Command / Query
db.students.find({
city: { $in: ["Delhi", "Mumbai", "Pune"] }
})
Expected Output
[
{
name: "Rahul",
city: "Delhi"
},
{
name: "Priya",
city: "Mumbai"
}
]
Explanation
MongoDB returns documents where city matches any value in the $in list.
Q4. Find Students Who Do Not Live in Selected Cities
Problem Statement
Find students who do not live in Delhi, Mumbai, or Pune.
MongoDB Command / Query
db.students.find({
city: { $nin: ["Delhi", "Mumbai", "Pune"] }
})
Expected Output
[
{
name: "Aman",
city: "Jaipur"
},
{
name: "Neha",
city: "Lucknow"
}
]
Explanation
$nin returns documents whose city does not match any value in the specified list.
Q5. Find Students With Selected Ages
Problem Statement
Find students whose age is 15, 16, or 18.
MongoDB Command / Query
db.students.find({
age: { $in: [15, 16, 18] }
})
Expected Output
[
{
name: "Priya",
age: 15
},
{
name: "Rahul",
age: 16
},
{
name: "Aman",
age: 18
}
]
Explanation
$in can be used with numbers as well as strings.
The query matches any student whose age is one of:
15, 16, 18
Q6. Find Students Whose Age Is Not 15 or 16
Problem Statement
Find students whose age is neither 15 nor 16.
MongoDB Command / Query
db.students.find({
age: { $nin: [15, 16] }
})
Expected Output
[
{
name: "Neha",
age: 17
},
{
name: "Aman",
age: 18
}
]
Explanation
$nin excludes documents whose age is 15 or 16.
Q7. Find Students Having Python or MongoDB as a Skill
Problem Statement
Find students whose skills array contains either Python or MongoDB.
MongoDB Command / Query
db.students.find({
skills: { $in: ["Python", "MongoDB"] }
})
Expected Output
[
{
name: "Rahul",
skills: ["Python", "MongoDB", "SQL"]
},
{
name: "Aman",
skills: ["Python", "HTML", "CSS"]
}
]
Explanation
When $in is used with an array field, MongoDB checks whether the array contains at least one of the specified values.
Q8. Find Students Who Do Not Have Python or Java in Their Skills
Problem Statement
Find students whose skills array contains neither Python nor Java.
MongoDB Command / Query
db.students.find({
skills: { $nin: ["Python", "Java"] }
})
Expected Output
[
{
name: "Neha",
skills: ["HTML", "CSS", "JavaScript"]
},
{
name: "Riya",
skills: ["C++", "SQL"]
}
]
Explanation
With an array field, $nin ensures that none of the specified values are present in the array.
Q9. Combine $in With Another Condition
Problem Statement
Find students who are studying either Python or Java and are older than 15.
MongoDB Command / Query
db.students.find({
course: { $in: ["Python", "Java"] },
age: { $gt: 15 }
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Aman",
age: 18,
course: "Java"
}
]
Explanation
Both conditions must be satisfied:
course = Python OR Java
AND
age > 15
Q10. Find Products Outside Selected Categories
Problem Statement
Suppose a products collection contains products from different categories. Find products whose category is not Electronics, Books, or Clothing.
MongoDB Command / Query
db.products.find({
category: {
$nin: ["Electronics", "Books", "Clothing"]
}
})
Expected Output
[
{
name: "Coffee Mug",
category: "Kitchen"
},
{
name: "Football",
category: "Sports"
}
]
Explanation
$nin is useful when you want to exclude several values from your search.
Here, products belonging to the three listed categories are excluded.
Key Takeaways
$inmatches a field against multiple possible values.$ninmeans not in and excludes specified values.$inreturns a document when at least one value matches.$ninMongoDB returns a document when none of the specified values match.$inMongoDB works with strings, numbers, and other supported values.$incan also be used with array fields.$ninMongoDB can be used to exclude multiple values from an array field.$inand$nincan be combined with other query conditions.$inis often easier to read than writing multiple$orconditions for simple equality checks.- Always verify your filter before using
$inor$ninwith update or delete operations.
FAQs
1. What is the $in operator in MongoDB?
$in is used to find documents where a field matches at least one value from a specified list.
db.students.find({
course: { $in: ["Python", "Java"] }
})
2. What is the $nin operator in MongoDB?
$nin means not in. It finds documents where a field does not match any value in the specified list.
db.students.find({
course: { $nin: ["Python", "Java"] }
})
3. What is the difference between $in and $nin?
$in includes documents matching any listed value, while $nin excludes documents matching any listed value.
$in → match these values
$nin → do not match these values
4. Can $in MongoDB be used with an array field?
Yes. $in can search for values inside an array field.
db.students.find({
skills: { $in: ["Python", "MongoDB"] }
})
This finds students whose skills array contains Python or MongoDB.
5. Can $nin MongoDB be used with an array field?
Yes. $nin can be used to find documents where an array does not contain any of the specified values.
db.students.find({
skills: { $nin: ["Python", "Java"] }
})
6. Can $in be combined with other MongoDB operators?
Yes. For example, $in can be combined with $gt.
db.students.find({
course: { $in: ["Python", "Java"] },
age: { $gt: 15 }
})
7. Is $in the same as $or in MongoDB?
For simple equality checks on the same field, $in can often be used instead of multiple $or conditions.
For example:
db.students.find({
course: { $in: ["Python", "Java"] }
})
is a concise way to match either course.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
