Array Data Type and Nested Array Values
A BSON array stores an ordered sequence of values. Arrays are useful for tags, skills, prerequisites, addresses, line items, and other related collections of values.
Example:
{
name: "Arun",
skills: ["JavaScript", "Node.js", "MongoDB"]
}
Arrays of scalar values
An array can contain strings, numbers, Booleans, dates, or other supported BSON values.
{
title: "MongoDB Fundamentals",
tags: ["database", "backend", "mongodb"]
}
A query can match a document by an array element:
db.courses.find({ tags: "mongodb" });
This finds documents whose tags array contains the string mongodb.
Matching multiple elements
db.courses.find({
tags: { $all: ["mongodb", "backend"] }
});
$all requires the array to contain all specified values. The elements do not need to be adjacent.
Exact array matching
db.courses.find({
tags: ["mongodb", "backend"]
});
This is not the same as searching for documents that contain both values anywhere in the array. Exact array equality considers the array's contents and order.
Arrays of embedded documents
Arrays can contain documents:
{
title: "MongoDB Mastery",
lessons: [
{ title: "Introduction", durationMinutes: 20 },
{ title: "Queries", durationMinutes: 45 }
]
}
You can query nested fields inside arrays, but conditions involving multiple properties of the same array element require careful handling. The $elemMatch operator is often useful for that purpose.
Unbounded array risks
An array that grows indefinitely can lead to oversized documents, increasingly expensive updates, and inefficient queries. MongoDB has a maximum BSON document size of 16 MiB.
For example, storing every historical event for a busy user inside a single user document is often a poor design. A separate event collection may be more appropriate.
Array order and duplicate values
Arrays preserve element order and can contain duplicate values. Whether duplicates are allowed should be determined by the application requirements and validation strategy.
Do not assume that a MongoDB array automatically behaves like a mathematical set.
db.arrayExamples.insertMany([
{
title: "MongoDB Basics",
tags: ["mongodb", "backend"],
lessons: [
{ title: "Introduction", durationMinutes: 20 },
{ title: "Queries", durationMinutes: 45 }
]
},
{
title: "React Basics",
tags: ["react", "frontend"],
lessons: [
{ title: "Components", durationMinutes: 30 }
]
}
]);
// Match one array element.
db.arrayExamples.find({ tags: "mongodb" });
// Require both values somewhere in the array.
db.arrayExamples.find({
tags: { $all: ["mongodb", "backend"] }
});
// Find an array element with a matching embedded field.
db.arrayExamples.find({
"lessons.title": "Queries"
});$all requires all specified values in the array. Dot notation can query nested fields within arrays of documents.- Arrays preserve order and can contain duplicate values.
- Queries can match array elements directly.
$allrequires the array to contain all specified values.- Exact array equality differs from element matching.
- Unbounded arrays can create document-size and performance problems.
What does `{ tags: { $all: ["mongodb", "backend"] } }` require?