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.