An array in MongoDB is a field that stores multiple values inside a single document. Arrays are useful for storing information such as student subjects, product tags, skills, phone numbers, and hobbies.
An array is an ordered collection of values enclosed
in square brackets []. An array can
contain strings, numbers, documents, or other arrays.
{
name: "Rahul",
age: 20,
subjects: ["Java", "Python", "HTML"]
}
In this document, subjects is an array
containing three values.
subjects[0] refers to "Java" conceptually.subjects[1] refers to "Python" conceptually.subjects[2] refers to "HTML" conceptually.
MongoDB queries use dot notation with numeric indexes
to access array elements, such as
"subjects.0".
We can store an array as a field when inserting a document.
db.array_students.insertOne({
name: "Rahul",
age: 20,
subjects: ["Java", "Python", "HTML"],
marks: [75, 85, 90]
})
The document contains two arrays:
subjects and marks.
db.array_students.insertMany([
{
name: "Priya",
subjects: ["HTML", "CSS", "JavaScript"],
skills: ["Design", "Programming"]
},
{
name: "Aman",
subjects: ["Python", "SQL"],
skills: ["Problem Solving", "Programming"]
}
])
Each student can have different values and different numbers of array elements.
MongoDB can match a document when an array contains the specified value.
db.array_students.find({
subjects: "Python"
})
This finds documents whose subjects
array contains the value "Python".
db.array_students.find({
skills: "Programming"
})
MongoDB matches the array element without requiring us to specify its index.
The $all operator matches documents
whose array contains every specified value.
Other array elements may also be present.
db.array_students.find({
subjects: {
$all: ["HTML", "CSS"]
}
})
This finds students whose subjects array contains both HTML and CSS, regardless of the order of those values.
db.array_students.find({
skills: {
$all: ["Design", "Programming"]
}
})
This matches documents containing both specified skills in the array.
The $in operator matches documents
when the field value matches at least one value
from the specified list. For an array field,
an element can match one of those values.
db.array_students.find({
subjects: {
$in: ["Python", "Java"]
}
})
This finds students whose subjects array contains Python or Java.
$all requires every specified value to be present.$in requires at least one specified value to match.
The $size operator matches arrays
containing exactly the specified number of elements.
db.array_students.find({
subjects: {
$size: 3
}
})
This finds students whose subjects array contains exactly three elements.
db.array_students.find({
skills: {
$size: 2
}
})
The $size query operator checks for an
exact array length. It does not directly support
ranges such as "at least three elements."
The $push operator adds a value to
an array. If the array field does not exist,
MongoDB creates it.
db.array_students.updateOne(
{ name: "Rahul" },
{
$push: {
subjects: "MongoDB"
}
}
)
The value "MongoDB" is appended to
Rahul's subjects array.
db.array_students.updateOne(
{ name: "Rahul" },
{
$push: {
marks: 95
}
}
)
This appends 95 to the marks array.
The $addToSet operator adds a value
only if an equal value is not already present
in the array.
db.array_students.updateOne(
{ name: "Rahul" },
{
$addToSet: {
subjects: "Python"
}
}
)
If Python is already present, this operation does not add another equal value. Otherwise, it adds Python.
Use $push when duplicate values are
acceptable, and $addToSet when you
want to avoid adding duplicate values.
The $pull operator removes all array
elements that match the specified value or condition.
db.array_students.updateOne(
{ name: "Rahul" },
{
$pull: {
subjects: "HTML"
}
}
)
This removes every occurrence of HTML from Rahul's subjects array.
db.array_students.updateOne(
{ name: "Rahul" },
{
$pull: {
marks: { $lt: 40 }
}
}
)
This removes all marks below 40 from the selected student's marks array.
The $pop operator removes one element
from an array:
-1 removes the first element.1 removes the last element.db.array_students.updateOne(
{ name: "Rahul" },
{
$pop: {
subjects: -1
}
}
)
db.array_students.updateOne(
{ name: "Rahul" },
{
$pop: {
subjects: 1
}
}
)
These operations remove an element from the selected student's subjects array.
MongoDB supports dot notation for matching a particular array position. Array indexes begin at zero.
db.array_students.find({
"subjects.0": "Java"
})
This finds documents where the first element of the subjects array is Java.
db.array_students.find({
"marks.1": 85
})
This finds documents where the second element of the marks array equals 85.
$all matches arrays containing all specified values.$in matches when at least one specified value matches.$size matches an exact array length.$push appends a value, including duplicates.$addToSet adds a value only if an equal value is not already present.$pull removes all matching array elements.$pop removes the first or last array element.
Create a collection named
practice_arrays and insert these documents:
db.practice_arrays.insertMany([
{
_id: 1,
name: "Rahul",
subjects: ["Java", "Python", "HTML"],
marks: [70, 85, 90]
},
{
_id: 2,
name: "Priya",
subjects: ["HTML", "CSS", "JavaScript"],
marks: [80, 90, 95]
},
{
_id: 3,
name: "Aman",
subjects: ["Python", "SQL"],
marks: [60, 75]
},
{
_id: 4,
name: "Neha",
subjects: ["Java", "CSS"],
marks: [88, 92]
}
])
$push.$addToSet.$pull.$pop.find().Run the insertion command only once, or clear the test collection before inserting the same sample data again.
1. Which operator adds a value to an array?
2. Which operator adds a value only if an equal value is not already present?
3. Which operator removes all matching array elements?