To delete a document (or Multiple Documents) in MongoDB.
we can use the "deleteOne()" method to delete a single document or the "deleteMany()" method to delete multiple documents that match specific criteria.
Alternatively, we can use the "findOneAndDelete()" method to find a single document and delete it atomically.
Here's how we can delete documents in MongoDB using the MongoDB shell:
Before creating a Collection(table) in MongoDB, First, we need to connect to our MongoDB instance using the MongoDB shell or a MongoDB client library using our programming language.
Using the Mongo shell, we can connect to a MongoDB server running on localhost by simply typing the `mongo` command in our terminal.
mongo
Once our connection is established successfully, we can check databases in MongoDB using the below command:
show dbs;
Switch to the database where you want to create the collection using the "use" command.
use your_database_name;
To check the current database name using mongo shell.
db
Use the "createCollection()" method to explicitly create a collection.
# Connect to MongoDB (assuming it's running locally on the default port) mongo # Switch to the desired database use your_database_name # Create a collection named your_collection_name db.createCollection("your_collection_name")
The above command will create a collection in our specified Database.
# Insert multiple documents into a collection db.your_collection_name.insertMany([ {name: "Alice", Department: "HR", age: 36, city: "London"}, {name: "Bella", Department: "Director", age: 32, city: "Polo Alto"}, {name: "Warner", Department: "Business Analyst", age: 30, city: "London"} ])
Use the appropriate delete method to remove the document(s) from the collection.
# Delete a single document from the collection db.your_collection_name.deleteOne({ _id: "ObjectId("xxxx1212asvs2xxxx")" }) // Delete document where _id is "ObjectId("xxxx1212asvs2xxxx")"
using the `deleteMany` method we can delete multiple documents from the collection in MongoDB.
// Delete document(s) where "department is HR" and "age is less than 30". db.your_collection_name.deleteMany({ department: "HR", age: { $lt: 30 } })
Replace "your_database_name" with the name of your database and "your_collection_name" with the name of your collection.
Adjust the filter criteria to match the documents you want to delete.
"deleteOne()" will delete the first document that matches the criteria.
while "deleteMany()" will delete all documents that match the criteria.
we can delete a single document and return the deleted document, for that we can use the "findOneAndDelete()" method.
# Find a document and delete it, returning the deleted document db.your_collection_name.findOneAndDelete({ name: "Alice", age: { $eq: 30 } }) // Above Statement will Find and delete document where name is "Alice" and age equal to "30".
This will find a document from collection name is equal to "Alice" and an `age` equal to `30` and delete it, and return either the deleted document or null if no document matches the criteria.