WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Delete Document


MongoDB - Delete Document : learn how to delete a document using MongoDB.

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.

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

There are following Basic syntax of remove() method
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Example

Consider the mycol collection has the following data.
{ "_id" : ObjectId(??4LLgt_QzPk), "title":"MongoDB Overview"}
{ "_id" : ObjectId(VsyR6t8Xf+%3), "title":"NoSQL Overview"}
{ "_id" : ObjectId(@JNq+W$f7N5B), "title":"Welookups"}
Following example will remove all the documents whose title is 'MongoDB Overview'.
   >db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(VsyR6t8Xf+%3), "title":"NoSQL Overview"}
{ "_id" : ObjectId(@JNq+W$f7N5B), "title":"Welookups"}
>
    

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

This is equivalent of SQL's truncate command.
>db.mycol.remove()
>db.mycol.find()
>




Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.