02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Deleting Documents, Collections and Databases

Deleting Documents, Collections, and Databases in MongoDB

In this tutorial, we will explore how to delete documents, collections, and databases in MongoDB. Knowing how to remove data effectively is crucial for managing your database and ensuring that only the most relevant information is retained.

Introduction

In our previous lessons, we learned how to create and find data in MongoDB. Now, we will look at how to remove data—whether it's a specific document, an entire collection, or even a complete database.

Note: Deleting data is a permanent action, so always proceed with caution and ensure you have appropriate backups in place.

Deleting Documents

MongoDB allows you to delete individual documents from a collection using the deleteOne() or deleteMany() methods.

Using deleteOne()

To delete a single document that matches a specified condition, use the deleteOne() method. For instance, if you want to delete a user named "Hen":

use MongoSample;
db.users.deleteOne({ name: "Hen" });

The command above will delete the first document it finds that matches the name "Hen".

Note: If there are multiple documents with the same name, only the first match will be deleted.

Using deleteMany()

If you need to delete all documents that match a given condition, use the deleteMany() method. For example, if you want to delete all users named "Ben":

db.users.deleteMany({ name: "Ben" });

This command will remove all documents where name is "Ben".

Deleting Collections

In some cases, you may need to delete an entire collection. This can be done with the drop() method.

Using drop() to Delete a Collection

To delete a collection, simply call the drop() method on it. For example, to delete the users collection:

db.users.drop();

After executing this command, the users collection will be permanently removed.

Warning: The drop() operation is irreversible. Once a collection is dropped, all the documents in that collection are gone forever.

Deleting Databases

If you want to delete an entire database, MongoDB provides a straightforward command to do this as well.

Using dropDatabase()

To delete the currently selected database, use the dropDatabase() method. For example:

use MongoSample;
db.dropDatabase();

The dropDatabase() command will delete the entire MongoSample database, including all its collections and documents.

Warning: Dropping a database is an irreversible action. Make sure you are deleting the correct database and that you have backups.

Practical Considerations

  • Backup Your Data: Always back up important data before performing any delete operations. Accidental deletions can lead to irreversible data loss if no backups are available.
  • Testing in a Development Environment: Before running delete commands on a production database, test them in a development environment to ensure they work as intended.

Summary

In this tutorial, we covered how to delete documents, collections, and databases in MongoDB:

  • Use deleteOne() or deleteMany() to remove specific documents.
  • Use drop() to delete entire collections.
  • Use dropDatabase() to delete a complete database.

Always be careful when performing delete operations, as they are often irreversible.

Important Update (2023): MongoDB 4.2 and later versions have added features like support for transactions, allowing you to execute multiple delete operations in an all-or-nothing manner. This helps in ensuring that either all delete actions are completed, or none are, providing better control over your data integrity.

Video Transcript

00:00Now that we know how to creative
00:02and find, it's time for us to learn how to remove things.
00:06We're gonna take a look at
00:07how do we actually remove documents from within collections?
00:10How do we get rid of a collection altogether?
00:13Or how do we drop that collection?
00:15And last but not least, we're gonna take a look at
00:17how do we completely drop or get rid of a complete database.
00:21So let's jump right into it.
00:24Obviously there's a lot of other things that you could do
00:27with MongoDB, but you know, for the basics
00:30and getting you up and running, this is more than enough
00:32to get you running and understanding how to work
00:34with MongoDB.
00:36With that said, I'm back into using MongoDB
00:38and I, what I wanna do is I want to delete a record,
00:42which is a document.
00:43I want to delete a collection,
00:45which is the only collection we created of users.
00:47And then I want delete our database itself of Mongo Sambo.
00:51So let's start, I'm gonna go ahead into my DB into users
00:55and let's find something.
00:57So let's find one. And this is the one that we found,
00:59for example, hen we want to get rid of.
01:01Hen the way we get rid of HEN is incredibly simple.
01:05The way we get rid of HEN is we call DB users
01:08and then I call the remove function.
01:10And in that remove function, I send that ID
01:14and I sign to it that object ID method here with the value
01:18that is returned.
01:20So lemme just put that right there
01:22and close our round bracket, click on enter,
01:25and I've just removed N.
01:27Now if I find one again, it's gonna get me something else
01:29because that specific item does not exist anymore.
01:33The hand that is 26 years old is no more.
01:36There is no hen that is 26. Now we only have three items.
01:40Beautiful. Now how do you get rid of a complete collection?
01:42And you know what? Let me just talk
01:43to you about one more thing, even
01:45before we get rid of the collections.
01:47I wanna show you how do we actually see the the
01:51what collections are there?
01:53That's a good question, right?
01:54Well, I'm actually not gonna answer it.
02:02Well, I'm kidding. Here it goes.
02:04So show collections, we'll give you a list
02:07of all the collections that exist.
02:09In this case, we only have one collection,
02:11but we really want to get rid of this collection.
02:14And if I go to DB users, I could just call the drop method
02:17and I would then get rid of that collection.
02:20If I try to see that collection. No collection anymore.
02:22We don't have a collection anymore.
02:24So that's how you get rid of collections.
02:26Last but not least, how do we get rid of a whole database?
02:29And to do that, I'm gonna approach the database directly
02:31and I'm gonna call drop data base.
02:34Now of course these are incredibly dangerous,
02:36especially if you've added
02:37and created all of your information.
02:39So be very careful when you're deleting things,
02:42but this is how you get rid of a complete database.
02:45In this lecture we learned how to get rid of documents,
02:48collections, and databases,
02:49and that takes us very close to the end of this section.
02:53But before we do that, in the next lecture,
02:55we're gonna go ahead and take a look at
02:57how do we actually integrate all this new things
02:59that we've learned about MongoDB.
03:01How do we pick a MongoDB driver
03:03and work with the directly within no js,
03:06which is really our goal.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!