MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Logical, Element & Array Query Operators ($and, $or, $exists, $all)0%
Study Materials
MongoDB & MongooseProgress 0%
1.1Document Databases vs Relational SQL: CAP Theorem & When to Use NoSQL1.2JSON vs BSON Data Types & MongoDB Internal Storage Engine (WiredTiger)1.3MongoDB Atlas Cloud Setup, Network Whitelisting & Compass GUI
2.1Insert Operations: insertOne, insertMany & Write Concerns2.2Find Queries & Comparison Operators ($eq, $gt, $in, $nin)2.3Logical, Element & Array Query Operators ($and, $or, $exists, $all)2.4Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)2.5Delete Operations, Cursors, Limit, Skip & Sorting in MongoDB
HomeTutorialsMongoDB & MongooseLogical, Element & Array Query Operators ($and, $or, $exists, $all)
Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)

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

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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:
1// Find courses that are either free (price: 0) OR have a top rating (>= 4.8)
2const featured = await db.collection('courses').find({
3 $or: [
4 { price: 0 },
5 { rating: { $gte: 4.8 } }
6 ]
7}).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:
1const query = await db.collection('users').find({
2 $and: [
3 { $or: [{ role: 'admin' }, { role: 'moderator' }] },
4 { $or: [{ isActive: true }, { isSuperUser: true }] }
5 ]
6}).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.
1// Find all users who have an optional 'phoneNumber' field
2const usersWithPhone = await db.collection('users').find({
3 phoneNumber: { $exists: true, $ne: null }
4}).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:
1// Match courses that teach BOTH 'react' AND 'nodejs'
2const fullstackCourses = await db.collection('courses').find({
3 tags: { $all: ['react', 'nodejs'] }
4}).toArray();
  • $size: Matches arrays with an exact number of elements:
1// Find articles with exactly 3 tags
2const threeTags = await db.collection('articles').find({
3 tags: { $size: 3 }
4}).toArray();
  • $elemMatch: Crucial for arrays of subdocuments! Ensures that a single subdocument satisfies multiple criteria:
1// Find students who have scored >= 90 on a 'Final Exam'
2const topStudents = await db.collection('students').find({
3 scores: {
4 $elemMatch: { type: 'Final Exam', score: { $gte: 90 } }
5 }
6}).toArray();

Interactive Quiz• Knowledge Check

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.

Related Lessons

Previous LessonNext Lesson
Find Queries & Comparison Operators ($eq, $gt, $in, $nin)Update Operations: updateOne, updateMany & Field Modifiers ($set, $inc, $push)

Practice Quiz

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

Mentor-Led Practical Training

Master MongoDB & Mongoose with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext