Logical, Element & Array Query Operators ($and, $or, $exists, $all)0%

Logical, Element & Array Query Operators ($and, $or, $exists, $all)

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Logical, Element & Array Query Operators ($and, $or, $exists, $all)

Beyond simple value comparisons, enterprise querying requires compound Boolean logic, schema existence checks, and precise array pattern matching. MongoDB provides robust Logical, Element, and Array query operators.


1. Logical Query Operators: $and, $or, $nor, $not

  • $or: Matches documents that satisfy at least one condition:
JavaScript
// Find courses that are either free (price: 0) OR have a top rating (>= 4.8)
const featured = await db.collection('courses').find({
$or: [
{ price: 0 },
{ rating: { $gte: 4.8 } }
]
}).toArray();
  • $and: Explicit Boolean AND. While specifying multiple fields in a filter object implicitly applies AND, $and is required when targeting the same field with multiple complex conditions or combining multiple $or clauses:
JavaScript
const query = await db.collection('users').find({
$and: [
{ $or: [{ role: 'admin' }, { role: 'moderator' }] },
{ $or: [{ isActive: true }, { isSuperUser: true }] }
]
}).toArray();

2. Element Operators: $exists & $type

Because MongoDB is schema-flexible, documents within the same collection may contain different fields:

  • $exists: Checks if a field is present in the document.
JavaScript
// Find all users who have an optional 'phoneNumber' field
const usersWithPhone = await db.collection('users').find({
phoneNumber: { $exists: true, $ne: null }
}).toArray();
  • $type: Matches documents where the field is of a specific BSON type (e.g. 'string', 'number', 'date', 'array').

3. Array Query Operators: $all, $size, $elemMatch

  • $all: Matches arrays that contain all specified elements, regardless of order or additional elements:
JavaScript
// Match courses that teach BOTH 'react' AND 'nodejs'
const fullstackCourses = await db.collection('courses').find({
tags: { $all: ['react', 'nodejs'] }
}).toArray();
  • $size: Matches arrays with an exact number of elements:
JavaScript
// Find articles with exactly 3 tags
const threeTags = await db.collection('articles').find({
tags: { $size: 3 }
}).toArray();
  • $elemMatch: Crucial for arrays of subdocuments! Ensures that a single subdocument satisfies multiple criteria:
JavaScript
// Find students who have scored >= 90 on a 'Final Exam'
const topStudents = await db.collection('students').find({
scores: {
$elemMatch: { type: 'Final Exam', score: { $gte: 90 } }
}
}).toArray();

Interactive Quiz

Multiple Choice Questions

Question 1
Which operator queries documents where an array field contains ALL of the specified elements?
Question 2
Why is $elemMatch necessary when querying arrays of embedded subdocuments?
Question 3
What does { discount: { $exists: false } } match?
Question 4
How do you query documents where status is either 'PENDING' OR 'IN_REVIEW'?
Question 5
What is the limitation of the $size operator in MongoDB queries?

Next Lesson

Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext