1.引入Jar包
org.springframework.boot
spring-boot-starter-data-mongodb
2.MongoDbHelper操作
/**
* MongoDB Operation class
* @author Mr.Li
* @date 2022-12-05
*/
public class MongoDbHelper {
@Autowired
private MongoTemplate mongoTemplate;
/**
* Save Individual Objects
*
* @param t
* @param
* @return
*/
public T save(T t) {
return mongoTemplate.insert(t);
}
/**
* Store the object to the specified collectionName
* @param objectToSave
* @param collectionName
* @param
* @return
*/
public T save(T objectToSave, String collectionName){
return mongoTemplate.insert(objectToSave,collectionName);
}
/**
* Batch save track data
*
* @param list
* @param collectionName
* @return
*/
public Collection batchSave(Collection list, String collectionName) {
return mongoTemplate.insert(list,collectionName);
}
/**
* Query Data
*
* @param query
* @param tClass
* @param
* @return
*/
public List find(Query query, Class tClass) {
return mongoTemplate.find(query, tClass);
}
/**
* Collection specified by query data
*
* @param query
* @param tClass
* @param
* @return
*/
public List find(Query query, Class tClass, String collectionName) {
return mongoTemplate.find(query, tClass,collectionName);
}
/**
* Pagination query
* @param query query criteria
* @param pageNum Current Page
* @param pageSize Number of entries per page
* @param sortField Sorted Field
* @param sortType 1:asc;0:desc
* @param tClass to class
* @param collectionName collection name
* @param
* @return
*/
public MongoPage findByPage(Query query,int pageNum,int pageSize,String sortField,int sortType, Class tClass, String collectionName) {
int count = (int) mongoTemplate.count(query, tClass, collectionName);
if(sortType==1){
query.with(Sort.by(Sort.Order.asc(sortField)));
}else {
query.with(Sort.by(Sort.Order.desc(sortField)));
}
//Set starting number
query.skip((pageNum - 1) * pageSize);
//Set the number of queries
query.limit(pageSize);
//Query the current page data set
List taskList = mongoTemplate.find(query, tClass,collectionName);
int size=count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
MongoPage page=new MongoPage();
page.setTotal(count);
page.setSize(size);
page.setData(taskList);
return page;
}
/**
* 查询前几条数据
* @param query
* @param limitNum 前几条
* @param sortField 排序字段
* @param sortType 0:倒序;1:正序
* @param tClass
* @param collectionName
* @param
* @return
*/
public List findTop(Query query,Integer limitNum,String sortField,int sortType, Class tClass, String collectionName){
if(sortType==1){
query.with(Sort.by(Sort.Order.asc(sortField)));
}else {
query.with(Sort.by(Sort.Order.desc(sortField)));
}
query.limit(limitNum);
return mongoTemplate.find(query, tClass,collectionName);
}
/**
* 查询一条数据
* @param query
* @param sortField
* @param sortType
* @param tClass
* @param collectionName
* @param
* @return
*/
public List findOne(Query query,String sortField,int sortType, Class tClass, String collectionName){
if(sortType==1){
query.with(Sort.by(Sort.Order.asc(sortField)));
}else {
query.with(Sort.by(Sort.Order.desc(sortField)));
}
//Set the number of queries
query.limit(1);
//Query the current page data set
List taskList = mongoTemplate.find(query, tClass,collectionName);
return taskList;
}
/**
* Query All
*
* @param tClass
* @param
* @return
*/
public List findAll(Class tClass) {
return mongoTemplate.findAll(tClass);
}
/**
* Query all specified collections
*
* @param tClass
* @param collectionName
* @param
* @return
*/
public List findAll(Class tClass,String collectionName) {
return mongoTemplate.findAll(tClass,collectionName);
}
/**
* create collection
* @param collName
* @param indexList
* @return
*/
public boolean createCollection(String collName, List
3.分页实体类MongoPage
/**
* MongoDB paged query returns result set
* @author Mr.Li
* @date 2023-01-11
*/
@Data
public class MongoPage {
/**
* Total number of data
*/
private Integer total;
/**
* Page count
*/
private Integer size;
/**
* Data result set per page
*/
private Object data;
}