MongoDB document deletion and mongodb document deletion skills
? ? 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(
,
)
? ? If your MongoDB version is later than 2.6, the syntax format is as follows:
db.collection.remove(
, { justOne:
, writeConcern:
})
Parameter description:
? ? · Query: (optional) Conditions for deleting objects.
? ? · 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: 'https: // 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": "https://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes ": 100} {"_ id": ObjectId ("5606616dade2f21f36b03138"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql Database", "": "cainiao tutorial", "url": "https://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes": 100}
? ? Next, we will remove the tilte document "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)
? ? To delete all data, use the following methods:
>db.col.remove({})>db.col.find()>
Note list
? ? The remove () method is outdated. Currently, the deleteOne () and deleteMany () methods are recommended.
? ? For example, to delete all documents under a collection:
db.inventory.deleteMany({})
? ? Delete all documents whose status is equal to:
db.inventory.deleteMany({ status : "A" })
? ? Delete a document whose status is D:
db.inventory.deleteOne( { status: "D" } )