</>
Skip to content
MongoDB lessons (17/38)

MongoDB — Array Operators

Element in Array

db.users.find({ hobbies: "reading" })

All Elements

db.users.find({ hobbies: { $all: ["reading", "gaming"] } })

Array Size

db.users.find({ hobbies: { $size: 2 } })

Element Match

db.users.find({
  scores: { $elemMatch: { subject: "math", score: { $gte: 90 } } }
})

Array Index

db.users.find({ "hobbies.0": "reading" })  // First element

Push to Array

db.users.updateOne(
  { name: "John" },
  { $push: { hobbies: "new hobby" } }
)

Pull from Array

db.users.updateOne(
  { name: "John" },
  { $pull: { hobbies: "old hobby" } }
)

Add to Set

db.users.updateOne(
  { name: "John" },
  { $addToSet: { hobbies: "unique hobby" } }
)

Mini Practice

  1. Query array elements
  2. Use $all operator
  3. Use $elemMatch
  4. Modify arrays

Up Next

Continue with Update Operators — update operators.

Related Topics

Frequently Asked Questions about Array Operators

What is Array Operators in MongoDB?

Array Operators is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Array Operators?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Array Operators.

Why is Array Operators important in MongoDB?

Array Operators is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.