
- 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
Deleting all records of a collection in MongoDB Shell?
To delete all records of a collection in MongoDB shell, use the remove() method. The syntax is as follows −
db.yourCollectionName.remove({});
To understand the syntax, let us create a collection with document. The query to create a collection with document is as follows −
> db.deleteAllRecordsDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f6ca32f684a30fbdfd596") } > db.deleteAllRecordsDemo.insertOne({"StudentName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f6cb22f684a30fbdfd597") } > db.deleteAllRecordsDemo.insertOne({"StudentName":"Mike","StudentAge":23,"Hobby":["Learning","Photography"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f6cde2f684a30fbdfd598") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.deleteAllRecordsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8f6ca32f684a30fbdfd596"), "StudentName" : "John" } { "_id" : ObjectId("5c8f6cb22f684a30fbdfd597"), "StudentName" : "Carol", "StudentAge" : 21 } { "_id" : ObjectId("5c8f6cde2f684a30fbdfd598"), "StudentName" : "Mike", "StudentAge" : 23, "Hobby" : [ "Learning", "Photography" ] }
Here is the query to delete all records of a collection in MongoDB:
> db.deleteAllRecordsDemo.remove({}); WriteResult({ "nRemoved" : 3 })
Look at the above query, we have deleted all records from a collection. If you try to get records from the above collection, you will get nothing.
The query is as follows −
> db.deleteAllRecordsDemo.find().pretty();
The following is the output −
>
- Related Articles
- Variable collection name in MongoDB shell with JavaScript?
- Get all fields names in a MongoDB collection?
- Get the maximum mark records from a collection with documents in MongoDB
- Set server status to inactive in a MongoDB collection with server records?
- Insert records in MongoDB collection if it does not exist?
- Is there a way to limit the number of records in a certain MongoDB collection?
- Get names of all keys in the MongoDB collection?
- Get the maximum mark records from a collection with documents in MongoDB query
- Deleting records in MySQL using Nodejs
- Fetching all documents from MongoDB Collection in a beautified form
- MongoDB Convert One record with an array to multiple records in a new collection?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- How to delete all the documents from a collection in MongoDB?
- MongoDB query to retrieve records from a collection named with letters and numbers
- Find all duplicate documents in a MongoDB collection by a key field?

Advertisements