Delete Operation
Delete a single document
shop> db.comments.deleteOne({_id: 1})
{ acknowledged: true, deletedCount: 1 }
Delete a field from the document
- It is basically update operation.
shop> db.comments.updateOne({_id:6}, {$unset: {metadata: 1}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
shop> db.comments.find({_id:6})
[
{
_id: 6,
title: 'Schema Design Best Practices',
content: 'Designing schemas for optimal performance...',
author: 'Emily Brown',
comments: [
{ user: 'Kevin', text: 'Invaluable insights!' },
{ user: 'Lily', text: 'Well-structured explanations.' }
],
status: false
}
]
Delete multiple documents
shop> db.products.deleteMany({price:55})
{ acknowledged: true, deletedCount: 6 }