$size, $all, $elemMatch, and positional operators for updates.
Query arrays
db.products.find({ tags: { $size: 2 } })
db.products.find({ tags: { $all: ['sale', 'new'] } })Practice: Use practice database in mongosh.
Positional update preview
db.products.updateOne(
{ sku: 'A1', 'tags.0': 'sale' },
{ $set: { 'tags.$': 'clearance' } }
)
filtered positional $[]
$[] and $[<identifier>] update all or filtered array elements—read docs before bulk array edits.
Important interview questions and answers
- Q: $size exact match?
A: Only matches arrays with exactly that length. - Q: Positional $ operator?
A: Updates first array element matching query path.
Self-check
- Find products with exactly two tags.
- What does $all require?
Tip: $all requires every listed value present in the array.
Interview prep
- $size?
- Matches arrays with exact element count.
- Positional $?
- Updates first matching array element.