MongoDB Delete Document - MongoDB

How to delete a document in MongoDB?

In this chapter, we will explain how to delete a document using MongoDB.

remove() Method

MongoDB's remove() method is used for removing a document from the collection. remove() method will accept two parameters. One is deletion criteria and the second is justOne flag.

  • deletion criteria − (Optional) deletion criteria according to documents will be removed.
  • justOne − (Optional) if set to true or 1, then it will remove only one document.

Syntax

Basic syntax of remove() method is as follows

Example

Consider mycol collection has the following data.

Below example removes all the documents whose title is 'MongoDB Overview'.

Remove Only One

If there are multiple records and if you want to delete only the first record, then set justOne parameter in remove() method.

Remove All Documents

If a deletion criterion is not specified, then MongoDB will delete entire documents from the collection. This is equivalent of SQL's truncate command.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MongoDB Topics