Arrays and embedded documents
Queries also reach inside arrays and nested documents.
Query inside arrays and nested documents with dot notation and $elemMatch, as in MongoDB.
Requirements
- A connection with a user that has
readon the database.
A nested field
JavaScript
db.customers.find({ "address.city": "Valencia" });An array element that meets several conditions
JavaScript
db.orders.find({ lines: { $elemMatch: { product: "coffee", quantity: { $gte: 2 } } } });An array that contains every value
JavaScript
db.products.find({ tags: { $all: ["eco", "sale"] } });Check
Each query returns only the documents whose array or embedded document meets the condition, whole unless you use a projection.
Common problems
- A condition on two fields of an array without
$elemMatchcan be met by different elements of the array.