MongoDB Delete document

Source: Internet
Author: User
Tags install mongodb mongodb tutorial mongodb version

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:

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.