MongoDB document deletion and mongodb document deletion skills

Source: Internet
Author: User
Tags mongodb tutorial mongodb version

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.