In the previous chapters, we have learned how to add and update data for collections in MongoDB. In this section we will continue to learn about the deletion of the MongoDB collection.
MongoDB
remove()
Function is used to remove data from the collection.
MongoDB data updates can be updated using the If your MongoDB is later than version 2.6, the syntax format is as follows: 参数说明: query : (optional) the condition of the deleted document. justOne : (optional) if set to true or 1, only one document is deleted, and if this parameter is not set, or if the default value false is used, all documents that match the criteria are deleted. writeConcern (optional) the level at which the exception is thrown We perform two insert operations in the following documents: Use Next, let’s remove the document whose title is’ MongoDB tutorial’: If you only want to delete the first record found, you can set justOne to 1, as shown below: If you want to delete all data, you can use the following methods (similar to the regular SQL truncate command):
update()
Function. In execution
remove()
Execute the function before the function
find()
Command to determine whether the execution conditions are correct, which is a good habit. 3.15.1. Grammar ¶
remove()
The basic syntax format of the method is as follows:db.collection.remove(
<query>,
<justOne>
)
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)
3.15.2. Example ¶
>db.col.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '菜鸟教程',
url: 'http://www.runoob.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
find()
Function to query data:> db.col.find()
{ "_id" : ObjectId("56066169ade2f21f36b03137"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5606616dade2f21f36b03138"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>db.col.remove({'title':'MongoDB 教程'})
WriteResult({ "nRemoved" : 2 }) # 删除了两条数据
>db.col.find()
…… # 没有数据
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
>db.col.remove({})
>db.col.find()
>