Examples of How to Remove Array Elements by Index in MongoDB
To remove array elements by index in MongoDB, we can use the $unset operator in combination with the positional operator $. The positional operator $ identifies the matched element in the array based on its index, allowing us to remove it from the array.
To understand How to Remove Array Elements by Index in MongoDB we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called users which contains the information shown below:
{ "_id": 1, "name": "Alice", "skills": ["JavaScript", "Python", "MongoDB", "Node.js"] }
{ "_id": 2, "name": "Bob", "skills": ["Java", "C++", "Python", "Ruby"] }
{ "_id": 3, "name": "Charlie", "skills": ["C#", "Java", "Python", "JavaScript"] }
{ "_id": 4, "name": "David", "skills": ["PHP", "JavaScript", "Python", "Ruby"] }
{ "_id": 5, "name": "Eve", "skills": ["Ruby", "Python", "Java", "C#"] }
{ "_id": 6, "name": "Frank", "skills": ["JavaScript", "C++", "Python", "PHP"] }
{ "_id": 7, "name": "Grace", "skills": ["Java", "Python", "Ruby", "JavaScript"] }
{ "_id": 8, "name": "Helen", "skills": ["Python", "C++", "Java", "PHP"] }
{ "_id": 9, "name": "Ivy", "skills": ["JavaScript", "Python", "Ruby", "C#"] }
{ "_id": 10, "name": "Jack", "skills": ["C++", "Java", "Python", "JavaScript"] }
1. Removing an Element by Index (Using $unset)
// Remove the third skill from the skills array of the document with _id 1
db.users.updateOne(
{ "_id": 1 },
{ "$unset": { "skills.2": 1 } }
);
Output
[
{
_id: 1,
name: 'Alice',
skills: [ 'JavaScript', 'Python', null, 'Node.js' ]
}
]
2. Removing All Occurrences of a Value from an Array (Using $pull)
// Remove all occurrences of "Java" from the skills array for all documents
db.users.updateMany(
{},
{ "$pull": { "skills": "Java" } }
);
Output
[
{
_id: 1,
name: 'Alice',
skills: [ 'JavaScript', 'Python', null, 'Node.js' ]
},
{ _id: 2, name: 'Bob', skills: [ 'C++', 'Python', 'Ruby' ] },
{ _id: 3, name: 'Charlie', skills: [ 'C#', 'Python', 'JavaScript' ] },
{
_id: 4,
name: 'David',
skills: [ 'PHP', 'JavaScript', 'Python', 'Ruby' ]
},
{ _id: 5, name: 'Eve', skills: [ 'Ruby', 'Python', 'C#' ] },
{
_id: 6,
name: 'Frank',
skills: [ 'JavaScript', 'C++', 'Python', 'PHP' ]
},
{ _id: 7, name: 'Grace', skills: [ 'Python', 'Ruby', 'JavaScript' ] },
{ _id: 8, name: 'Helen', skills: [ 'Python', 'C++', 'PHP' ] },
{
_id: 9,
name: 'Ivy',
skills: [ 'JavaScript', 'Python', 'Ruby', 'C#' ]
},
{ _id: 10, name: 'Jack', skills: [ 'C++', 'Python', 'JavaScript' ] }
]
3. Adding a New Skill to an Array
// Add "SQL" to the skills array of the document with _id 2
db.users.updateOne(
{ "_id": 2 },
{ "$push": { "skills": "SQL" } }
);
Output
[ { _id: 2, name: 'Bob', skills: [ 'C++', 'Python', 'Ruby', 'SQL' ] } ]
4. Removing a Document by _id
// Remove the document with _id 3
db.users.deleteOne({ "_id": 3 });
Output
{ acknowledged: true, deletedCount: 1 }
5. Querying Documents with a Specific Skill
// Find all documents that have "JavaScript" in their skills array
db.users.find({ "skills": "JavaScript" });
Output
[
{
_id: 1,
name: 'Alice',
skills: [ 'JavaScript', 'Python', null, 'Node.js' ]
},
{
_id: 4,
name: 'David',
skills: [ 'PHP', 'JavaScript', 'Python', 'Ruby' ]
},
{
_id: 6,
name: 'Frank',
skills: [ 'JavaScript', 'C++', 'Python', 'PHP' ]
},
{ _id: 7, name: 'Grace', skills: [ 'Python', 'Ruby', 'JavaScript' ] },
{
_id: 9,
name: 'Ivy',
skills: [ 'JavaScript', 'Python', 'Ruby', 'C#' ]
},
{ _id: 10, name: 'Jack', skills: [ 'C++', 'Python', 'JavaScript' ] }
]
6. Querying Documents by _id
// Find the document with _id 4
db.users.findOne({ "_id": 4 });
Output
{
_id: 4,
name: 'David',
skills: [ 'PHP', 'JavaScript', 'Python', 'Ruby' ]
}
How to Remove Array Elements by Index in MongoDB
In MongoDB, arrays are a flexible data structure that allows storing multiple values within a single document field. Occasionally, we may need to remove specific elements from an array based on their index.
In this article, we’ll explore how to remove array elements by index in MongoDB by providing detailed explanations, examples, and outputs to help beginners understand and implement this operation effectively.