querydrill

Learn ›MongoDB fundamentals ›Nested objects and arrays

Arrays of objects

arraysfind()nested fields

An array of objects is the shape you meet most often in real data - order line items, addresses, comments. Dot notation reaches into it, but what it matches is looser than it looks, which is what the next lesson is about.

Example:

{
  name: "Denish",

  orders: [
    {
      product: "Laptop",
      price: 1000,
      status: "completed"
    },
    {
      product: "Mouse",
      price: 50,
      status: "pending"
    }
  ]
}

Basic matching

db.users.find({
  "orders.product": "Laptop"
})

Finds the document if any order has product Laptop.