In MongoDB, data is stored in a structure consisting of databases, collections, and documents. A document is a record that contains data, a collection is a group of documents, and a database is like a container that holds multiple collections.
In a MongoDB server, there exist multiple databases containing multiple collections and multiple documents. These occupy some space in the server memory. Also, developers often work with a local copy of the MongoDB database when testing or manipulating the database and there are situations where developers may need to delete the data present on the live database server.
The documents, collections or databases can be deleted easily in MongoDB. Deleting data will help clear out irrelevant, useless or temporary data. In this tutorial, we’ll explore the different ways to delete data in MongoDB, including deleting everything. Let’s start with a short introduction to MongoDB.
What is MongoDB?
MongoDB is a document-oriented database management system that allows developers to store mammoth amounts of data. It is open-source and very efficient. It is a NoSQL-type database that is it does not store data inside tables but as documents inside collections.
MongoDB Inc. is the brain behind the MongoDB database. It has developed the database system and currently manages it. It was first launched in February 2009 and is managed under SSPL (Server Side Public License).
The database management system has gained immense popularity because of its driver support for most of the popular languages such as Nodejs, PHP, Java, Go, .Net, C, C++, Python, Ruby Scala, C#, Perl, Swift, Motor, and Mongoid. It backs as a database management system for applications built on these languages.
Companies like Facebook, Google, Adobe, Nokia, and many other top companies have chosen MongoDB to store their huge amounts of data.
How to Delete Everything in a MongoDB Database
For deleting everything in a MongoDB database, such as the documents in a collection, a collection inside of a database, or the database itself, a user can use different methods respectively. Let’s see them one by one.
Deleting Documents in MongoDB Collections
Follow the steps enlisted below for deleting documents using the MongoDB shell.
- Start your MongoDB database server. If you don’t have the .bat file to start the MongoDB server, open a new file in VS Code. Paste this code:
@echo off
cd "C:\mongodb\bin"
start mongod.exe
timeout 4
start mongo.exe
exit
Now, save the file on the desired location as a Windows Batch file (.bat). The file name can be anything like StartMongo.bat.
- Now, you can start the MongoDB database server by running the bat file. You should now have the Mongo shell open automatically for you.
- To view a list of all your databases, pass the below command.
show dbs
- You should now see a list of databases available in your database. Let us suppose we found a database named countrymade-agro.
- Now, move into the database using the below command.
use countrymade-agro
- In it, we have the reginasRanch collection. To view all collections, execute the below command.
show collections
- To be able to use this collection, you need to move into it. Execute the below command for that.
use reginasRanch
You are now inside the reginasRanch collection.
Let’s say we have some data stored here about the animals we breed on the ranch, in the form of documents. Now, we want to delete some documents from this collection may be because some of our animals died, got sold, or for some other reason.
Deleting documents can be done in different ways. We might want to delete just one document or may want to delete many, inside a collection. If we want, we can pass a condition to MongoDB and let it know what it should look for in the document(s) to be deleted.
Once a condition is matched to a few documents, we might want to delete all documents from a collection. Or, we may just want to just delete one or delete all documents that match a given parameter. Otherwise, we just might want to clear out our collection altogether and start afresh.
Below is the query for each condition.
- To delete all documents inside a collection:
db.reginasRanch.deleteMany({})
- To delete all documents inside a collection that match a given condition:
db.reginasRanch.deleteMany( { animal: "Horse" } )
- To delete a single document that matches a condition:
db.reginasRanch.deleteOne( { name: "Blue" } )
Note: When we pass an empty curly brace blank without a filter, MongoDB understands that we want to target everything inside the collection without following any criteria. Please be extra careful while using the delete operation with blank curly braces.
We have a separate tutorial on deleteOne and deleteMany methods for removing documents, click here to read.
Deleting a Collection in MongoDB
Let’s say Regina’s Ranch is closing and we need to delete its collection. To delete an entire collection inside a database, the syntax is given below.
db.reginasRanch.drop()
Deleting an Entire Database in MongoDB
We can also delete the entire database which deletes all the collections in MongoDB and eventually remove all documents associated with those collections.
For example, let’s say CountryMade Agro itself is shutting down. Now, we need to delete the entire countrymade-agro database. For that, first Move into the countrymade-agro database.
use countrymade-agro
Then simply drop it by specifying the below command.
db.dropDatabase()
Read More: Migrating WordPress Content to MongoDB
Summary
A MongoDB server can have multiple databases, a database can have multiple collections, and a collection can have a number of documents, for deleting each of them we have to execute a separate query having a different method. For instance, to delete documents match a specific filter we can use deleteMany() method, if we use this method without a filter, it will remove all documents inside a specified collection, and if we want to delete only one document we can use deleteOne() method. For deleting a complete collection we can use a default method drop(), and to delete an entire database we can use dropDatabase() method.
Reference
MongoDB Manual on Delete Operation