MongoDB update document

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

MongoDB update document

Update () method

The update () method is used to update an existing document. The syntax format is as follows:

db.collection.update(   <query>,   <update>,   {     upsert: <boolean>,     multi: <boolean>,     writeConcern: <document>   })

Parameter description:

  • Query: the query condition of the update statement, similar to the where clause after the SQL update statement.
  • Update: The update object and some updated operators (such as $, $ inc...) can also be understood as
  • Upsert: Optional. This parameter indicates whether to insert objNew if no update record exists. true indicates insertion. The default value is false.
  • Multi: Optional. The default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all multiple records are updated according to the condition.
  • WriteConcern: (optional) exception level.
Instance

We insert the following data into the set col:

> Db. col. insert ({title: 'mongodb ', description: 'mongodb is a Nosql database', by: 'cainiao ', url: 'http: // www.runoob.com ', tags: ['mongodb ', 'database', 'nosql'], likes: 100 })

Then we use the update () method to update the title ):

> Db. col. update ({'title': 'mongodb '}, {$ set: {'title': 'mongos'}) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) # output information> db. col. find (). pretty () {"_ id": ObjectId ("56064f89ade2f21f36b03136"), "title": "MongoDB", "description": "MongoDB is a Nosql Database", "": "cainiao tutorial", "url": "http://www.runoob.com", "tags": ["mongodb", "database", "NoSQL"], "likes": 100}>

The title is updated from the original "MongoDB tutorial" to "MongoDB ".

The preceding statement only modifies the first detected document. If you want to modify multiple identical documents, set the multi parameter to true.

> Db. col. update ({'title': 'mongodb '}, {$ set: {'title': 'mongos' }}, {multi: true })
Save () method

The save () method replaces an existing document by passing in the document. The syntax format is as follows:

db.collection.save(   <document>,   {     writeConcern: <document>   })

Parameter description:

  • Document: document data.
  • WriteConcern: (optional) exception level.
Instance

In the following example, we replace the document data with the _ id 56064f89ade2f21f36b03136:

> Db. col. save ({"_ id": ObjectId ("56064f89ade2f21f36b03136"), "title": "MongoDB", "description": "MongoDB is a Nosql Database", "": "Runoob", "url": "http://www.runoob.com", "tags": ["mongodb", "NoSQL"], "likes": 110 })

After the replacement is successful, we can use the find () command to view the replaced data.

> Db. col. find (). pretty () {"_ id": ObjectId ("56064f89ade2f21f36b03136"), "title": "MongoDB", "description": "MongoDB is a Nosql Database", "": "Runoob", "url": "http://www.runoob.com", "tags": ["mongodb", "NoSQL"], "likes": 110}>
More instances

Update only the first record:

Db. col. update ({"count": {$ gt: 1 }},{$ set: {"test2": "OK "}});

Update all:

Db. col. update ({"count": {$ gt: 3 },{$ set: {"test2": "OK" }}, false, true );

Add only the first entry:

Db. col. update ({"count": {$ gt: 4 }},{ $ set: {"test5": "OK" }}, true, false );

Add all:

Db. col. update ({"count": {$ gt: 5 }},{ $ set: {"test5": "OK" }}, true, true );

Update all:

Db. col. update ({"count": {$ gt: 15 },{$ inc: {"count": 1 }}, false, true );

Update only the first record:

Db. col. update ({"count": {$ gt: 10 },{$ inc: {"count": 1 }}, false, false );

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.