Delete Document in MongoDB
Remove () method in MongoDB
The remove () method in MongoDB is used to delete the Document from Collection. The remove () method takes two parameters. The first parameter of the deletion criteria determines the Document to delete, and the second parameter is justOne.
- deletion criteria : (Optional) Specify the Document to delete.
- justOne : (Optional) If set to true or 1, only delete a Document.
Syntax
The basic syntax of remove () method is as follows:
> db . COLLECTION_NAME . remove ( DELLETION_CRITTERIA )
For example
You monitor the Collection named mycol with the following data:
{ "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" }
The following example will delete all documents whose title is 'MongoDB Overview':
> db . mycol . remove ({ 'title' : 'MongoDB Overview' }) > db . mycol . find () { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } >
Only delete a Document in MongoDB
If there are multiple records and you only want to delete the first record, set the justOne parameter in the remove () method.
> db . COLLECTION_NAME . remove ( DELETION_CRITERIA , 1 )
Delete all Documents in MongoDB
If you do not specify the deletion criteria, MongoDB will delete all documents from Collection.This is equivalent to the truncate command in SQL .
> db . mycol . remove () > db . mycol . find () >
According to Tutorialspoint
Previous article: Update Document in MongoDB
Next article: Projection in MongoDB
You should read it
May be interested
- Instructions on 2 ways to install MongoDB on Raspberry Piin this tutorial, tipsmake will guide you through the process of installing and setting up the mongodb server software on your raspberry pi.
- Reference Database in MongoDBas shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.
- Delete Database in MongoDBthe db.dropdatabase () command in mongodb is used to delete an existing database.
- Limit records in MongoDBto limit the records in mongodb, you need to use the limit () method. the limit () method takes a parameter in a numeric format, which is the document number you want to display.
- Advantages of MongoDBany relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
- Data type in MongoDBmongodb supports many different data types. the following are some typical data types.
- MongoDB malicious code attacks more than 26,000 victims in a weekmalware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
- Covered Query in MongoDBwhen all fields in the query are part of the index, mongodb connects query conditions and returns the result by using the same index without looking inside the document. when indexes are present in ram, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.
- Learn about security features and authentication in MongoDBin the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
- Insert Document in MongoDBto insert data into collection in mongodb, you need to use the insert () or save () method.