Skip to content
Justice Tech School

Querying Arrays

A filter on an array field matches if any element matches. $all needs every value, $size matches length and $elemMatch matches one element against several conditions.

Example
db.posts.find({ tags: "mongodb" })
db.posts.find({ tags: { $all: ["db", "nosql"] } })
db.orders.find({ items: { $elemMatch: { sku: "A1", qty: { $gt: 2 } } } })

Exercise

Array length.

{ tags: { $: 3 } }

Quiz Test

When use $elemMatch?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.