[Spring Data MongoDB] learning notes -- modify template insertion and modification operations, mongodb integrates spring

Source: Internet
Author: User
Tags findone mongodb collection

[Spring Data MongoDB] learning notes -- modify template insertion and modification operations, mongodb integrates spring

Insert operation:

Directly give an example

Import static org. springframework. data. mongodb. core. query. Criteria. where; import static org. springframework. data. mongodb. core. query. Criteria. query ;... Person p = new Person ("Bob", 33); employee template. insert (p); // you can add another parameter to provide collectionname. For example, insert (p, "person"). Person qp = your template. findOne (query (where ("age"). is (33), Person. class );

The name of collection document can be as follows:

1. The default class name is lower-case. For example, com. test. Person-> person

2. Add @ Document to the class name.

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

 

Main Operations:

Insert, insertAll, save (when the object does not exist, execute insert ).

 

Update operation:

import static org.springframework.data.mongodb.core.query.Criteria.where;import static org.springframework.data.mongodb.core.query.Query;import static org.springframework.data.mongodb.core.query.Update;          ...  WriteResult wr = mongoTemplate.updateMulti(new Query(where("accounts.accountType").is(Account.Type.SAVINGS)),                                                            new Update().inc("accounts.$.balance", 50.00),                                                            Account.class);

Main Operations:

UpdateFirst (update the first matched), updateMulti (update all matched)

 

Upsert operation:

template.upsert(query(where("ssn").is(1111).and("firstName").is("Joe").and("Fraizer").is("Update")), update("address", addr), Person.class);

First query. If no matching conditions are met, insert will be executed. The inserted value is the query value + update value.

 

FindAndModify operation:

mongoTemplate.insert(new Person("Tom", 21));mongoTemplate.insert(new Person("Dick", 22));mongoTemplate.insert(new Person("Harry", 23));Query query = new Query(Criteria.where("firstName").is("Harry"));Update update = new Update().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 the newly updated document when updatingp = template.findAndModify(query, update, new FindAndModifyOptions().returnNew(true), Person.class);assertThat(p.getAge(), is(25));

You can set different execution effects by setting FindAndModifyOptions.

Query query2 = new Query(Criteria.where("firstName").is("Mary"));p = mongoTemplate.findAndModify(query2, update, 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 objects.


An error occurred while inserting an array object in the spring data mongodb collection.

It seems like it overflows.

Which jar package does orgspringframeworkdatadocumentmongodbworkflow template belong?

This is a mongoDB package. You can go to the next one on the official website. The addresses of the latest template in M2 and M4 versions are different. You can try it yourself.

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.