Deleting Data into Cosmos DB for MongoDB

One of the most basic operations is deleting data in a collection. In this guide, we will cover everything you need to know about deleting data using the Mongo Shell (Mongosh).

Understanding the deleteOne() and deleteMany() Methods

The most common way to delete data in MongoDB is to delete individual documents from a collection. You can do this using the deleteOne() or deleteMany() method.

The deleteOne() method is used to delete a single document from a collection that matches a specific filter. For example, if you wanted to delete a user with the name "John Doe" from the "users" collection, you would use the following command:

db.users.deleteOne({ "name": "John Doe" })

The deleteMany() method, on the other hand, is used to delete multiple documents from a collection that match a specific filter. For example, if you wanted to delete all users with an age less than 30 from the "users" collection, you would use the following command:

db.users.deleteMany({ "age": { $lt: 30 } })

It's important to note that both of these methods return an object with the following properties:

deletedCount: The number of documents deleted. acknowledged: This property will be true.

Deleting a Collection

To delete an entire collection, use the drop() method. For example, if you wanted to delete the "users" collection, you would use the following command:

db.users.drop()
This will delete the "users" collection and all of its documents permanently.

Next steps