1.引入依賴包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
2.yml文件配置
spring:
application:
name: credit #服務名稱
data:
mongodb:
uri: mongodb://賬號:密碼@地址:端口/數據庫名
# uri: mongodb://name:pass@localhost:27017/test
# uri: mongodb://192.168.1.1:20000,192.168.1.2:20000,192.168.252.12:20000/test
3.使用
集合數據結構
public class TestColl {
private String _id;
private String name;
private String sex;
private int age;
}
單元測試demo
package com.lykj.credit;
import com.lykj.credit.bean.TestColl;
import com.mongodb.client.result.UpdateResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.CollectionOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.List;
import java.util.Map;
/**
* @version 0.4
* @describe:
* @author: houkai
* @Date: 2019/11/20 16:52
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CreditApplication.class)
@WebAppConfiguration
public class MongoTest {
@Autowired
private MongoTemplate mongoTemplate;
/**創建集合*/
@Test
public void collTest() {
//創建集合
CollectionOptions collectionOptions = new CollectionOptions(102400L, 1000000L, false);
mongoTemplate.createCollection("collTest", collectionOptions);
//刪除集合
mongoTemplate.dropCollection("collTest");
}
/** 新增文檔*/
@Test
public void insertTest() {
TestColl testColl = new TestColl();
testColl.setAge(19);
testColl.setName("藏嗚嗚");
testColl.setSex("女");
TestColl result = mongoTemplate.insert(testColl, "testColl");
System.out.printf(result.toString());
}
/**更新文檔*/
@Test
public void updateTest() {
Query query = new Query(Criteria.where("name").is("藏嗚嗚"));
Update update = new Update().set("age", 99).set("sex", "男");
//更新查詢返回結果集的第一條
UpdateResult result = mongoTemplate.updateFirst(query, update, "testColl");
System.out.printf("匹配行數:" + result.getMatchedCount() + "修改行數:" + result.getModifiedCount());
//更新查詢返回結果集的所有
mongoTemplate.updateMulti(query, update, "testColl");
}
/**刪除文檔*/
@Test
public void deleteTestById() {
Query query = new Query(Criteria.where("_id").is("5ddb3295d7e36bfaf1dd0054"));
mongoTemplate.remove(query, MongoTest.class);
}
/**查詢文檔 */
@Test
public void findTestByName() {
Query query = new Query(Criteria.where("name").is("zhangsan"));
TestColl mgt = mongoTemplate.findOne(query, TestColl.class, "testColl");
System.out.printf(mgt.toString());
}
/**count 查詢*/
@Test
public void countTest() {
Query query = new Query();
long result = mongoTemplate.count(query, TestColl.class, "testColl");
System.out.printf(result + "");
}
/**分頁查詢*/
@Test
public void pageTest() {
int page = 0;
int size = 10;
page = (page -1) < 0 ? 0: page -1;
Query query = new Query().skip(page * size).limit(size);
List<TestColl> list = mongoTemplate.find(query, TestColl.class, "testColl");
long totalSize = mongoTemplate.count(new Query(), TestColl.class, "testColl");
System.out.println("result:" + list.toString());
System.out.println("count:" + totalSize);
}
/**排序查詢*/
@Test
public void sortTest(){
Query query = new Query();
query.with(Sort.by(new Sort.Order(Sort.Direction.DESC, "age")));
List<TestColl> testColls = mongoTemplate.find(query, TestColl.class,"testColl");
System.out.printf(testColls.toString());
}
/** 聚合查詢 */
@Test
public void aggregationTest(){
String alias = "nowNum";
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("name").is("ss")),
Aggregation.group("age").count().as(alias)
);
AggregationResults<Map> results= mongoTemplate.aggregate(aggregation, "testColl", Map.class);
List<Map> mappedResults = results.getMappedResults();
if (mappedResults != null && mappedResults.size() > 0) {
Integer num = (Integer) mappedResults.get(0).get(alias);
System.out.printf("num:" + num);
}
}
}