Array Operators in MongoDB

From the MongoDB cheat sheet · Query Operators · verified Jul 2026

Array Operators

Query and match array fields

javascript
// Match array containing value
db.posts.find({ tags: "mongodb" })

// Match array with all values
db.posts.find({ 
  tags: { $all: ["mongodb", "database"] }
})

// Match array by size
db.users.find({ 
  hobbies: { $size: 3 }
})

// Match array element
db.orders.find({
  "items.product": "laptop"
})
🟢 Essential for working with arrays
💡 Simple value matches any array element
📌 $elemMatch for complex array element conditions
⚡ Index array queries for better performance
⚠️ $size doesn't accept ranges (use $where)
queryoperatorsarrays

More MongoDB tasks

Back to the full MongoDB cheat sheet