3.15. MongoDB deletes a document

发布时间 :2025-10-25 12:33:00 UTC      

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 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>
)

If your MongoDB is later than version 2.6, the syntax format is as follows:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

参数说明:

  • 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

3.15.2. Example

We perform two insert operations in the following documents:

>db.col.insert({title: 'MongoDB 教程',
    description: 'MongoDB 是一个 Nosql 数据库',
    by: '菜鸟教程',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

Use 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 }

Next, let’s remove the document whose title is’ MongoDB tutorial’:

>db.col.remove({'title':'MongoDB 教程'})
WriteResult({ "nRemoved" : 2 })           # 删除了两条数据
>db.col.find()
……                                        # 没有数据

If you only want to delete the first record found, you can 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 methods (similar to the regular SQL truncate command):

>db.col.remove({})
>db.col.find()
>
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.