- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
MongoDB - Java
- MongoDB - Java Setup
- MongoDB - Java - Create Collection
- MongoDB - Java - Get Collection
- MongoDB - Java - Insert Document
- MongoDB - Java - Retrieve Documents
- MongoDB - Java - Update Document
- MongoDB - Java - Delete Document
- MongoDB - Java - Drop Collection
- MongoDB - Java - List Collections
MongoDB - PHP
- MongoDB - PHP Setup
- MongoDB - PHP - Create Collection
- MongoDB - PHP - Get Collection
- MongoDB - PHP - Insert Document
- MongoDB - PHP - Retrieve Documents
- MongoDB - PHP - Update Document
- MongoDB - PHP - Delete Document
- MongoDB - PHP - Drop Collection
- MongoDB - PHP - List Collections
MongoDB - Advanced
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
MongoDB - Useful Resources
MongoDB - Delete Document
In this chapter, we will learn how to delete a document using MongoDB.
The deleteOne() Method
MongoDB's deleteOne() method is used to remove a document from the collection. deleteOne() method accepts deletion criteria.
Syntax
Basic syntax of deleteOne() method is as follows −
>db.COLLECTION_NAME.deleteOne(DELLETION_CRITTERIA)
Example
Consider the mycol collection has the following data.
[
{
_id: ObjectId('6964b8da25c4ea6b911e2629'),
title: 'New MongoDB Tutorial',
description: 'MongoDB is no SQL database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: [ 'mongodb', 'database', 'NoSQL' ],
likes: 100
},
{
_id: ObjectId('6964b8da25c4ea6b911e262a'),
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: [ 'mongodb', 'database', 'NoSQL' ],
likes: 20,
comments: [
{
user: 'user1',
message: 'My first comment',
dateCreated: ISODate('2013-12-09T21:05:00.000Z'),
like: 0
}
]
}
]
Following example will delete the document whose title is 'NoSQL Database'.
sampleDB>db.mycol.deleteOne({'title':'NoSQL Database'})
{ acknowledged: true, deletedCount: 1 }
sampleDB> db.mycol.find()
[
{
_id: ObjectId('6964b8da25c4ea6b911e2629'),
title: 'New MongoDB Tutorial',
description: 'MongoDB is no SQL database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: [ 'mongodb', 'database', 'NoSQL' ],
likes: 100
}
]
The remove() Method
MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag.
deletion criteria − (Optional) deletion criteria according to documents will be removed.
justOne − (Optional) if set to true or 1, then remove only one document.
Syntax
Basic syntax of remove() method is as follows −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Consider the mycol collection has the following data.
[
{
_id: ObjectId('6964b8da25c4ea6b911e2629'),
title: 'New MongoDB Tutorial',
description: 'MongoDB is no SQL database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: [ 'mongodb', 'database', 'NoSQL' ],
likes: 100
},
{
_id: ObjectId('6964b8da25c4ea6b911e262a'),
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: [ 'mongodb', 'database', 'NoSQL' ],
likes: 20,
comments: [
{
user: 'user1',
message: 'My first comment',
dateCreated: ISODate('2013-12-09T21:05:00.000Z'),
like: 0
}
]
}
]
Following example will remove all the documents whose title is 'MongoDB Overview'.
sampleDB>db.mycol.remove({'title':'New MongoDB Tutorial'})
{ acknowledged: true, deletedCount: 1 }
sampleDB> db.mycol.find()
[
{
_id: ObjectId('6964b8da25c4ea6b911e262a'),
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: [ 'mongodb', 'database', 'NoSQL' ],
likes: 20,
comments: [
{
user: 'user1',
message: 'My first comment',
dateCreated: ISODate('2013-12-09T21:05:00.000Z'),
like: 0
}
]
}
]
Remove Only One
If there are multiple records and you want to delete only the first record, then set justOne parameter in remove() method.
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
Remove All Documents
If you don't specify deletion criteria, then MongoDB will delete whole documents from the collection. This is equivalent of SQL's truncate command.
> db.mycol.remove({})
WriteResult({ "nRemoved" : 2 })
> db.mycol.find()
>