
- 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
Delete all elements in an array field in MongoDB?
You can use $set operator for this. The syntax is as follows −
db.yourCollectionName.update({}, { $set : {"yourFieldName": [] }} , {multi:true} );
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.deleteAllElementsInArrayDemo.insertOne({"InstructorName":"Larry","InstructorTechnicalSubject":["Java","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fb971d3c9d04998abf00e") } > db.deleteAllElementsInArrayDemo.insertOne({"InstructorName":"Mike","InstructorTechnicalSubject":["C","C++","Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8fb98ad3c9d04998abf00f") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.deleteAllElementsInArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8fb971d3c9d04998abf00e"), "InstructorName" : "Larry", "InstructorTechnicalSubject" : [ "Java", "MongoDB" ] } { "_id" : ObjectId("5c8fb98ad3c9d04998abf00f"), "InstructorName" : "Mike", "InstructorTechnicalSubject" : [ "C", "C++", "Python" ] }
Here is the query to delete all elements in the array field −
> db.deleteAllElementsInArrayDemo.update({}, { $set : {"InstructorTechnicalSubject": [] }} , {multi:true} ); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Let us now check all the documents from a collection using find(). The query is as follows −
> db.deleteAllElementsInArrayDemo.find().pretty();
The following is the output. We have deleted all the elements of the “InstructorTechnicalSubject” array field:
{ "_id" : ObjectId("5c8fb971d3c9d04998abf00e"), "InstructorName" : "Larry", "InstructorTechnicalSubject" : [ ] } { "_id" : ObjectId("5c8fb98ad3c9d04998abf00f"), "InstructorName" : "Mike", "InstructorTechnicalSubject" : [ ] }
- Related Articles
- Delete All Odd Elements from an Array in Java
- How to project specific elements in an array field with MongoDB?
- Delete a field and value in MongoDB?
- How to delete element from an array in MongoDB?
- How to pull all elements from an array in MongoDB without any condition?
- Update elements inside an array in MongoDB?
- Update multiple elements in an array in MongoDB?
- Reverse array field in MongoDB?
- Delete specific record from an array nested within another array in MongoDB?
- Add a field to an embedded document in an array in MongoDB?
- Get count of array elements from a specific field in MongoDB documents?
- How to delete elements from an array?
- Replace an array field value with MongoDB?
- Minimum delete operations to make all elements of array same in C++.
- C Program to delete the duplicate elements in an array

Advertisements