Spring Data MongoDB Four: Basic document Modification (update) (i)

Source: Internet
Author: User
Tags mongodb add mongodb update

Spring Data MongoDB III: Basic document Queries (query, basicquery) (i)

Learn MongoDB II: MongoDB Add, delete, modify


A. Introduction

Spring Data MongoDB provides org.springframework.data.mongodb.core.MongoTemplate operation of the update to MongoDB, which can be a string for a set key that is a key-value pair when storing data. Values can be any type in the collection of data types, including arrays and documents for modification, and today we introduce the methods and parameters for modifying the basic document.

Our basic documentation for MONGODB modifies the query syntax of MongoDB:

  >db.collection.update (   <query>,   <update>,   upsert:<boolean>,   multi:< boolean>)


Parameters

Type

Describe

Query

Document

which query criteria to modify, similar to the SQL of the where

update

document

The value of the field to be modified

upsert

boolean

optional, The default value is false true, Span style= "COLOR: #555555" >insert False,

Multi

Boolean

Optional, the default value is false . If the corresponding multiple records are found based on the query criteria, if set to false , only the first bar is modified, and if set to true, all updates

We springdata MongoDB to provide the corresponding modification method

1. Mongotemplate. Updatefirst Modify the first record that matches the condition

2. Mongotemplate. Updatemulti Modify all of the eligible

3. Mongotemplate. Upsert when a condition is modified if it does not exist

Parameter description:

(1) Query: What query criteria to modify, similar to where in SQL

1) org.springframework.data.mongodb.core.query

2) Org.springframework.data.mongodb.core.query.BasicQuery

The difference between these two parameters and the use of the spring Data MongoDB III: Basic document Queries (query, Basicquery) (a) has been introduced, in detail


(2) Update

1) org.springframework.data.mongodb.core.query.Update

2) Sub-class Org.springframework.data.mongodb.core.query.BasicUpdate


Org.springframework.data.mongodb.core.query.BasicUpdate inherited Org.springframework.data.mongodb.core.query.Update.


Update provides some ways to work with basic documents

Spring MongoDB Update

Mongodb

Describe

Update Rename (string oldname, String newName)

$rename

Rename Field

Update set (String key, Object value)

$set

Used to specify the value of a key and create it if it does not exist

Update unset (String key)

$unset

Used to specify the value of a key if it does not exist create it without creating it

Basicupdate inherits the Update method, and basicupdate constructs the parameter to implement the update SQL statement itself

1) basicupdate (Dbobjectupdate object) object is in JSON format

2) basicupdate (java.lang.StringupdateString)

basicupdate need to be implemented manually $set such as operator SQL statements, you can also modify the operation method of the document using some of the update operations, because the update class inherits the

two. Mongotemplate.upsert Operation

Mongotemplate. Upsert when a condition is modified if it does not exist


1. If the modification meets the conditions when the operation is not saved

The first step: Our query condition onumber=001 cname to modify, Spring Datamongodb code implementation

     Mongotemplate.upsert (NewQuery (Criteria.where ("Onumber"). Is ("001")), Newupdate (). Set ("CNAME", "zcy"), CollectionName);

The second step: we first query MongoDB data

    >db.orders.find ({"Onumber": "001"})    

Step three: We perform mongotemplate.upsert operations

Fourth step: Query MongoDB data:

   >db.orders.find ({"Onumber": "001"})     {"_id": ObjectId ("55c5673e28121ca9e1dd397f"), "Onumber": "001", "CNAME": "Zcy"}


If it does not exist when it is modified, it is equivalent to executing the insert

2. Modifying a field that does not exist when the modification meets the criteria


The first step: the Spring Data MongoDB code does not change

The second step: We first query MongoDB data :

   > Db.orders.find ({"Onumber": "001"})     {"_id": ObjectId ("55c5689727e0a66301f9bb51"), "Onumber": "001"}


Step three: We perform mongotemplate.upsert operations


Fourth step: Then query the MONGODB data:

  >db.orders.find ({"Onumber": "001"})     {"_id": ObjectId ("55c5689727e0a66301f9bb51"), "Onumber": "001", "CNAME": "Zcy"}


Equivalent to the implementation of MongoDB:

  >db.orders.update (                                 {"Onumber": "001"},       {$set: {"CNAME": "Zcy2"}},        true,        true    

Verified the mongotemplate. Upsert when a condition is modified if it does not exist

three. Mongotemplate. Updatefirst

Mongotemplate. Updatefirst Modify the first record that matches the condition

1. Actions to modify multiple records when a condition is met

The first step: we modify the date of the query condition Cname=zcy, the Spring Data MongoDB code implements

       Mongotemplate. Updatefirst (NewQuery (Criteria.where ("CNAME"). Is ("Zcy")), Newupdate (). Set ("Date", "2015-08-08"), CollectionName);

The second step: we first query MongoDB data


Step three: Execute mongotemplate. Updatefirst operation


Fourth step: Then look up MongoDB data:


Four. Mongotemplate. Updatemulti

Mongotemplate.updatemulti Modify all of the eligible


1. Actions to modify multiple records when a condition is met

The first step: we modify the date of the query condition Cname=zcy, the Spring Data MongoDB code implements

       Mongotemplate. Updatemulti (NewQuery (Criteria.where ("CNAME"). Is ("Zcy")), Newupdate (). Set ("Date", "2015-08-08"), CollectionName);


The second step: we first query MongoDB data

Step three: Execute mongotemplate. Updatemulti operation


Fourth step: Query MongoDB data:

Modify all data documents that match the criteria

Four. Basicupdate Operation

Basicupdate JSON format, we need to implement the update SQL ourselves,basicupdate need to implement manually $set and other operator SQL statements, You can also use some of the update's actions to modify the document's Operation method, because the update class inherits the

Mongotemplate.updatefirst Modify the first record that matches the condition

The first step: When modifying multiple records when the condition is met, our query condition Cname=zcy's date is modified, and the Spring Data MongoDB code implements

      Basicdbobject basicdbobject=new basicdbobject ();      Basicdbobject.put ("$set", New Basicdbobject ("date", "2015-08-09"));      Updateupdate=newbasicupdate (basicdbobject);      Mongotemplate.updatefirst (New Query (Criteria.where ("CNAME"). Is ("Zcy")), update,collectionname);


The second step: querying MongoDB data


Step three: Perform the mongotemplate.updatefirst operation.

Fourth step: Querying MongoDB data


Basicdbobject can modify multiple fields at the same time


Today introduced the basic document modification, only the Update set (String key, Object value) operation, the other operators can refer to Learning MongoDB II: MongoDB Add, delete, modify, we mainly introduce the modification method today, The other operations are similar to the set method, so there is no detail about the past.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Data MongoDB Four: Basic document Modification (update) (i)

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.