
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to remove an array element by its index in MongoDB?
You can remove an array element by its index using the following two steps −
The first step is as follows −
db.yourCollectionName.update({}, {$unset : {"yourArrayFieldName.yourIndexValue" : 1 }});
The above syntax will put a null value at the location of ‘yourIndexValue’. After that, you need to pull the null value from array filed to remove from an array element.
The second step is as follows −
db.yourCollectionName.update({}, {$pull : {"yourArrayFieldName" : null}});
To implement the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.removeArrayElementByItsIndexDemo.insertOne({"InstructorName":"David", "InstructorAge":28,"InstructorSubject":["MongoDB","MySQL","Java","SQL Server","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8abbfc6cea1f28b7aa0803") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.removeArrayElementByItsIndexDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8abbfc6cea1f28b7aa0803"), "InstructorName" : "David", "InstructorAge" : 28, "InstructorSubject" : [ "MongoDB", "MySQL", "Java", "SQL Server", "PL/SQL" ] }
Here is the query to remove an array element by its index.
Step 1 − The query is as follows −
> db.removeArrayElementByItsIndexDemo.update({}, {$unset : {"InstructorSubject.2" : 1 }}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Step 2 − The query is as follows −
> db.removeArrayElementByItsIndexDemo.update({}, {$pull : {"InstructorSubject" : null}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the array element “Java” has been removed or not. The query is as follows −
> db.removeArrayElementByItsIndexDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8abbfc6cea1f28b7aa0803"), "InstructorName" : "David", "InstructorAge" : 28, "InstructorSubject" : [ "MongoDB", "MySQL", "SQL Server", "PL/SQL" ] }
Look at the sample output, the array element “Java” has been removed completely.
- Related Articles
- How do you remove an array element by its index in MongoDB
- How to remove array element in MongoDB?
- How to remove an element from a list by index in Python?
- How to remove element in a MongoDB array?
- MongoDB query to match and remove element from an array?
- How to get the index of an array element in older versions on MongoDB?
- How to remove a specific element from array in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?
- Remove null element from MongoDB array?
- How to remove an element from an array in Java
- Get a single element from the array of results by index in MongoDB
- How to delete element from an array in MongoDB?
- How to push an element into array in MongoDB?
- How to remove an element from Array List in C#?
- Remove values in an array by comparing the items 0th index in JavaScript?
