MongoDB - Java

MongoDB - PHP

MongoDB - Advanced

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()
>
Advertisements