Visit complete MongoDB roadmap

← Back to Topics List

deleteOne() and others

When working with MongoDB, you will often need to delete documents or even entire collections to manage and maintain your database effectively. MongoDB provides several methods to remove documents from a collection, allowing for flexibility in how you choose to manage your data. In this section, we will explore key delete methods in MongoDB and provide examples for each.

db.collection.deleteOne()

The deleteOne() method is used to delete a single document from a collection. It requires specifying a filter that selects the document(s) to be deleted. If multiple documents match the provided filter, only the first one (by natural order) will be deleted.

Syntax: db.collection.deleteOne(FILTER)

Example:

db.users.deleteOne({ firstName: 'John' });

This command will delete the first users document found with a firstName field equal to "John".

db.collection.deleteMany()

The deleteMany() method is used to remove multiple documents from a collection. Similar to deleteOne(), it requires specifying a filter to select the documents to be removed. The difference is that all documents matching the provided filter will be removed.

Syntax: db.collection.deleteMany(FILTER)

Example:

db.users.deleteMany({ country: 'Australia' });

This command will delete all users documents with a country field equal to "Australia".

db.collection.remove()

The remove() method can be used to delete documents in a more flexible way, as it takes both a filter and a justOne option. If justOne is set to true, only the first document (by natural order) that matches the filter will be removed. Otherwise, if justOne is set to false, all documents matching the filter will be deleted.

Syntax: db.collection.remove(FILTER, JUST_ONE)

Example:

db.users.remove({ age: { $lt: 18 } }, true);

This command would delete a single user document with an age field value less than 18.

db.collection.drop()

In cases where you want to remove an entire collection, including the documents and the metadata, you can use the drop() method. This command does not require a filter, as it removes everything in the specified collection.

Syntax: db.collection.drop()

Example:

db.users.drop();

This command would delete the entire users collection and all related data.

It’s important to note that these methods will remove the affected documents permanently from the database, so use caution when executing delete commands. Keep in mind to keep backups or use version control to maintain data integrity throughout the lifecycle of your MongoDB database.

Community

roadmap.sh is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube

roadmap.sh by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · FAQs · Terms · Privacy

ThewNewStack

The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.