MongoDB Delete document
The MongoDB remove () function is used to remove data from the set.
You can use the update () function to update MongoDB data. It is a good habit to execute the find () command before executing the remove () function to determine whether the execution conditions are correct.
Syntax
The basic syntax format of the remove () method is as follows:
db.collection.remove( <query>, <justOne>)
If your MongoDB version is later than 2.6, the syntax format is as follows:
db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> })
Parameter description:
- Query: (optional) Conditions for deleting a document.
- JustOne: (optional) Delete only one document if set to true or 1.
- WriteConcern: (optional) exception level.
Instance
We perform the following two insert operations:
> Db. col. insert ({title: 'mongodb ', description: 'mongodb is a Nosql database', by: 'cainiao ', url: 'http: // www.runoob.com ', tags: ['mongodb ', 'database', 'nosql'], likes: 100 })
Use the find () function to query data:
> Db. col. find () {"_ id": ObjectId ("56066169ade2f21f36b03133"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database ", "by": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100} {"_ id": ObjectId ("5606616dade2f21f36b03138"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database", "": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes": 100}
Next, we will remove the document with the title of "MongoDB tutorial:
> Db. col. remove ({'title': 'mongodb tutorial '}) WriteResult ({"nRemoved": 2}) # two data items are deleted> db. col. find ()...... # No data
If you only want to delete the first record, set justOne to 1, as shown below:
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
If you want to delete all data, you can use the following method (similar to the truncate command of conventional SQL ):
>db.col.remove({})>db.col.find()>
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: