[Spring Data MongoDB] Learning Note--mongotemplate Insert Modify action

Source: Internet
Author: User
Tags findone

Insert operation:

Give an example directly

Import Static Org.springframework.data.mongodb.core.query.Criteria.where; Import Static  New person ("Bob", "= Mongotemplate.findone" ("The Where (" "Age"), the person.  Class);  

There are several ways to name collection document:

1. The default is the class name, and the first letter is lowercase. such as Com.test.Person, person

2. Specify by adding the tag @document to the class name.

3. When performing the operation, pass the CollectionName as a parameter.

Main operation:

Insert,insertall, save (executes insert when object does not exist).

Update operation:

Import StaticOrg.springframework.data.mongodb.core.query.Criteria.where;Import StaticOrg.springframework.data.mongodb.core.query.Query;Import Staticorg.springframework.data.mongodb.core.query.Update;  ... Writeresult WR= Mongotemplate.updatemulti (NewQuery (WHERE ("Accounts.accounttype"). is (Account.Type.SAVINGS)),NewUpdate (). Inc ("Accounts.$.balance", 50.00), account.class);

Main operation:

Updatefirst (update first match), Updatemulti (update all matches)

Upsert Operation:

Template.upsert (Query (WHERE ("SSN"). Is (1111). and ("FirstName"). Is ("Joe"). and ("Fraizer"). Is ("Update"), Update (" Address ", addr), person. class);

The first query, if there is no qualifying, will perform the insert, the inserted value is the query value + update value.

Findandmodify Operation:

Mongotemplate.insert (NewPerson ("Tom", 21)); Mongotemplate.insert (NewPerson ("Dick", 22)); Mongotemplate.insert (NewPerson ("Harry", 23)); Query Query=NewQuery (Criteria.where ("FirstName"). Is ("Harry"))); Update Update=NewUpdate (). Inc ("age", 1); Person P= mongotemplate.findandmodify (query, update, person.)class);//return ' s old Person objectAssertthat (P.getfirstname (), is ("Harry.")); Assertthat (P.getage (), is (23));p= Mongotemplate.findone (query, person.class); Assertthat (P.getage (), is (24));//Now return to the newly updated document when updatingp = template.findandmodify (query, UPDATE,NewFindandmodifyoptions (). Returnnew (true), person.class); Assertthat (P.getage (), is (25));

You can set a different execution effect by setting findandmodifyoptions.

New Query (Criteria.where ("FirstName"). Is ("Mary"new findandmodifyoptions (). Returnnew (true ). Upsert (true), person. class ); Assertthat (P.getfirstname (), is ("Mary")); Assertthat (P.getage (), is (1));

The remove operation can be used to delete.

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.