MongoDB – Insert, Update, Delete Document
In MongoDB, documents are inserted, updated, and deleted from collections using simple commands. Below are examples of how to insert, update, and delete documents in MongoDB.
1. Insert Document
You can insert a document into a collection using one of the following methods: insertOne() (for a single document) or insertMany() (for multiple documents).
Insert a Single Document
db.customers.insertOne({
_id: 1,
firstName: "John",
lastName: "Doe",
dateOfBirth: new Date("1985-04-15"),
contactInfo: {
phone: "123-456-7890",
email: "john.doe@example.com",
address: "123 Elm Street, Springfield"
},
policies: []
})
This command inserts a single document into the customers collection.
Insert Multiple Documents
db.customers.insertMany([
{
_id: 2,
firstName: "Jane",
lastName: "Smith",
dateOfBirth: new Date("1990-03-10"),
contactInfo: {
phone: "987-654-3210",
email: "jane.smith@example.com",
address: "456 Oak Street, Springfield"
},
policies: []
},
{
_id: 3,
firstName: "Alice",
lastName: "Johnson",
dateOfBirth: new Date("1980-11-20"),
contactInfo: {
phone: "654-321-0987",
email: "alice.johnson@example.com",
address: "789 Pine Street, Springfield"
},
policies: []
}
])
This command inserts multiple documents into the customers collection at once.
2. Update Document
You can update a document in MongoDB using the updateOne(), updateMany(), or replaceOne() methods.
Update a Single Document
To update a single document based on a condition, you can use updateOne(). This updates the first matching document.
db.customers.updateOne(
{ _id: 1 }, // Condition to match the document
{
$set: {
"contactInfo.phone": "111-222-3333", // New value for the phone number
"contactInfo.address": "789 New Street, Springfield" // Updated address
}
}
)
In this example, the document with _id: 1 will have its phone number and address updated.
Update Multiple Documents
To update multiple documents that match a condition, use updateMany(). For example, to change the address for all customers living in Springfield:
db.customers.updateMany(
{ "contactInfo.address": /Springfield/ }, // Condition
{
$set: {
"contactInfo.city": "New Springfield" // Updating city field
}
}
)
This will update the city field to “New Springfield” for all customers whose address contains “Springfield”.
Replace a Document
To replace an entire document, you can use replaceOne(). This will replace the document with the new one completely.
db.customers.replaceOne(
{ _id: 2 }, // Condition
{
_id: 2,
firstName: "Jane",
lastName: "Smith",
dateOfBirth: new Date("1990-03-10"),
contactInfo: {
phone: "999-999-9999",
email: "jane.smith@newdomain.com",
address: "123 New Oak Street, Springfield"
},
policies: []
}
)
This will replace the entire document of customer with _id: 2 with the new data.
3. Delete Document
You can delete documents from a collection using deleteOne() or deleteMany() methods.
Delete a Single Document
To delete a single document, you can use deleteOne(). For example, deleting a customer based on their _id:
db.customers.deleteOne({ _id: 1 })
This command will delete the document where _id: 1.
Delete Multiple Documents
To delete multiple documents, you can use deleteMany(). For example, to delete all customers from a certain city:
db.customers.deleteMany({ "contactInfo.city": "New Springfield" })
This command deletes all documents from the customers collection where the city is “New Springfield”.
Summary of Commands
Operation | Command | Example |
---|---|---|
Insert one | insertOne() | db.customers.insertOne({…}) |
Insert many | insertMany() | db.customers.insertMany([{}, {}]) |
Update one | updateOne() | db.customers.updateOne({_id: 1}, {$set: {…}}) |
Update many | updateMany() | db.customers.updateMany({condition}, {$set: {…}}) |
Replace one | replaceOne() | db.customers.replaceOne({_id: 1}, {…}) |
Delete one | deleteOne() | db.customers.deleteOne({_id: 1}) |
Delete many | deleteMany() | db.customers.deleteMany({condition}) |
Conclusion
MongoDB provides simple and flexible commands to insert, update, and delete documents in collections. These operations allow you to manage your data effectively in MongoDB, whether you’re adding new documents, updating existing ones, or removing data that’s no longer needed.
Recent Comments