
- 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 do you remove an array element by its index in MongoDB
To remove array element by its index in MongoDB, you can use $unset and $pull operator. There are two steps to remove array elements from an array.The syntax for the same is as follows:
db.yourCollectionName.update({},{$unset:{"yourArrayListName.yourPosition":yourPositionValue}}; db.yourCollectionName.update({},{$pull:{"yourArrayListName":null}});
To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:
>db.removeArrayElements.insertOne({"StudentName":"Larry","StudentAge":23,"TechnicalSub ject":["C","C++","Java","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ea4879c4643706aef56d2") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.removeArrayElements.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ea4879c4643706aef56d2"), "StudentName" : "Larry", "StudentAge" : 23, "TechnicalSubject" : [ "C", "C++", "Java", "MongoDB" ] }
Here is the query to remove array element by its index:
> db.removeArrayElements.update({},{$unset:{"TechnicalSubject.3":1}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.removeArrayElements.update({},{$pull:{"TechnicalSubject":null}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Above, we have removed array element “MongoDB”, which is at index 3. Let us display documents from a collection. The query is as follows:
> db.removeArrayElements.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ea4879c4643706aef56d2"), "StudentName" : "Larry", "StudentAge" : 23, "TechnicalSubject" : [ "C", "C++", "Java" ] }
Look at the above sample output, the index 3 i.e. position 4 has been removed from the array i.e. the element “MongoDB”.
- Related Articles
- How to remove an array element by its index in MongoDB?
- How do you limit an array sub-element 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?
- How do you perform an AND query on an array in MongoDB?
- How do you get the index of an element in a list in Java?
- How do I remove a particular element from an array in JavaScript
- Remove null element from 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 do I remove a string from an array in a MongoDB document?
- How to remove a specific element from array in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?
- Get a single element from the array of results by index in MongoDB
