一、mongotemplate概述
1.1 什么是mongotemplate
mongotemplate是spring data mongodb提供的核心类,它封装了mongodb的java驱动,提供了丰富的方法来操作mongodb数据库。相比直接使用mongodb驱动,mongotemplate提供了更简洁、更符合spring风格的api。
主要特性:
- 丰富的crud操作
- 强大的查询构建器
- 聚合框架支持
- 网格文件系统(gridfs)支持
- 声明式事务支持
- 自动类型转换
1.2 核心依赖配置
<!-- pom.xml -->
<dependencies>
<!-- spring boot starter data mongodb -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-mongodb</artifactid>
</dependency>
<!-- 如果需要reactive支持 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-data-mongodb-reactive</artifactid>
</dependency>
<!-- lombok简化代码 -->
<dependency>
<groupid>org.projectlombok</groupid>
<artifactid>lombok</artifactid>
<optional>true</optional>
</dependency>
</dependencies>1.3 配置文件
# application.yml
spring:
data:
mongodb:
# 单机配置
host: localhost
port: 27017
database: testdb
username: admin
password: secret
# 连接池配置
auto-index-creation: true
authentication-database: admin
# 副本集配置
# uri: mongodb://user:pass@host1:port1,host2:port2/database?replicaset=rs0
# 连接超时配置
connection-per-host: 100
min-connections-per-host: 10
max-wait-time: 120000
connect-timeout: 10000
socket-timeout: 0
# ssl配置
ssl:
enabled: false
# 监控配置
mongodb:
metrics:
enabled: true二、mongotemplate核心配置
2.1 基础配置类
@configuration
@enablemongoauditing // 启用审计功能
public class mongoconfig {
@value("${spring.data.mongodb.database}")
private string database;
@value("${spring.data.mongodb.host}")
private string host;
@value("${spring.data.mongodb.port}")
private int port;
/**
* 方式一:使用属性配置创建mongoclient
*/
@bean
public mongoclient mongoclient() {
connectionstring connectionstring = new connectionstring(
string.format("mongodb://%s:%d/%s", host, port, database)
);
mongoclientsettings settings = mongoclientsettings.builder()
.applyconnectionstring(connectionstring)
.applytoconnectionpoolsettings(builder ->
builder.maxsize(100) // 连接池最大连接数
.minsize(10) // 最小连接数
.maxwaittime(2, timeunit.minutes) // 最大等待时间
)
.applytosocketsettings(builder ->
builder.connecttimeout(10, timeunit.seconds) // 连接超时
.readtimeout(30, timeunit.seconds) // 读取超时
)
.applytosslsettings(builder ->
builder.enabled(false) // 禁用ssl
)
.codecregistry(codecregistries.fromregistries(
mongoclientsettings.getdefaultcodecregistry(),
codecregistries.fromproviders(pojocodecprovider.builder()
.automatic(true)
.build())
))
.build();
return mongoclients.create(settings);
}
/**
* 方式二:使用mongotemplate bean
*/
@bean
public mongotemplate mongotemplate(mongoclient mongoclient) {
return new mongotemplate(mongoclient, database);
}
/**
* 配置自定义转换器
*/
@bean
public mongocustomconversions customconversions() {
list<converter<?, ?>> converters = new arraylist<>();
converters.add(new localdatetimetostringconverter());
converters.add(new stringtolocaldatetimeconverter());
converters.add(new bigdecimaltodecimal128converter());
converters.add(new decimal128tobigdecimalconverter());
return new mongocustomconversions(converters);
}
/**
* 自定义类型映射
*/
@bean
public mappingmongoconverter mappingmongoconverter(
mongodatabasefactory factory,
mongomappingcontext context,
mongocustomconversions conversions) {
dbrefresolver dbrefresolver = new defaultdbrefresolver(factory);
mappingmongoconverter converter = new mappingmongoconverter(dbrefresolver, context);
converter.setcustomconversions(conversions);
converter.settypemapper(new defaultmongotypemapper(null)); // 不保存_class字段
// 配置字段映射策略
converter.setmapkeydotreplacement("_"); // 将map key中的点替换为下划线
return converter;
}
/**
* 自定义转换器示例
*/
public class localdatetimetostringconverter implements converter<localdatetime, string> {
@override
public string convert(localdatetime source) {
return source.format(datetimeformatter.iso_local_date_time);
}
}
public class stringtolocaldatetimeconverter implements converter<string, localdatetime> {
@override
public localdatetime convert(string source) {
return localdatetime.parse(source, datetimeformatter.iso_local_date_time);
}
}
}2.2 多数据源配置
@configuration
public class multimongoconfig {
/**
* 主数据源配置
*/
@configuration
@enablemongorepositories(
basepackages = "com.example.repository.primary",
mongotemplateref = "primarymongotemplate"
)
public static class primarymongoconfig {
@bean
@primary
@configurationproperties(prefix = "spring.data.mongodb.primary")
public mongoproperties primaryproperties() {
return new mongoproperties();
}
@primary
@bean(name = "primarymongoclient")
public mongoclient primarymongoclient() {
mongoproperties properties = primaryproperties();
return mongoclients.create(
mongoclientsettings.builder()
.applyconnectionstring(new connectionstring(properties.geturi()))
.build()
);
}
@primary
@bean(name = "primarymongotemplate")
public mongotemplate primarymongotemplate() {
return new mongotemplate(primarymongoclient(),
primaryproperties().getdatabase());
}
}
/**
* 次数据源配置
*/
@configuration
@enablemongorepositories(
basepackages = "com.example.repository.secondary",
mongotemplateref = "secondarymongotemplate"
)
public static class secondarymongoconfig {
@bean
@configurationproperties(prefix = "spring.data.mongodb.secondary")
public mongoproperties secondaryproperties() {
return new mongoproperties();
}
@bean(name = "secondarymongoclient")
public mongoclient secondarymongoclient() {
mongoproperties properties = secondaryproperties();
return mongoclients.create(
mongoclientsettings.builder()
.applyconnectionstring(new connectionstring(properties.geturi()))
.build()
);
}
@bean(name = "secondarymongotemplate")
public mongotemplate secondarymongotemplate() {
return new mongotemplate(secondarymongoclient(),
secondaryproperties().getdatabase());
}
}
}2.3 实体类映射配置
/**
* 基础实体类
*/
@data
@noargsconstructor
@allargsconstructor
@superbuilder
@document(collection = "base_entities")
@inheritance // 支持继承
public abstract class baseentity {
@id
private string id;
@createddate
@field("created_at")
private localdatetime createdat;
@lastmodifieddate
@field("updated_at")
private localdatetime updatedat;
@createdby
@field("created_by")
private string createdby;
@lastmodifiedby
@field("updated_by")
private string updatedby;
@version
private long version;
@field("is_deleted")
private boolean isdeleted = false;
/**
* 软删除标记
*/
public void softdelete() {
this.isdeleted = true;
this.updatedat = localdatetime.now();
}
}
/**
* 用户实体类
*/
@data
@equalsandhashcode(callsuper = true)
@document(collection = "users")
@compoundindexes({
@compoundindex(name = "idx_username_email", def = "{'username': 1, 'email': 1}"),
@compoundindex(name = "idx_created_at", def = "{'createdat': -1}")
})
@typealias("user") // 类型别名
public class user extends baseentity {
@indexed(unique = true, background = true)
@field("username")
private string username;
@indexed
@field("email")
@email
private string email;
@field("age")
@min(0)
@max(150)
private integer age;
@field("address")
private address address;
@field("roles")
private set<string> roles = new hashset<>();
@field("metadata")
private map<string, object> metadata = new hashmap<>();
@field("profile")
private userprofile profile;
@field("status")
@dbref(lazy = true) // 懒加载引用
private status status;
// 自定义转换器
@field("custom_field")
@persistenceconstructor
public user(string id, string username, string email) {
this.id = id;
this.username = username;
this.email = email;
}
}
/**
* 地址值对象
*/
@data
@noargsconstructor
@allargsconstructor
@builder
public class address {
@field("street")
private string street;
@field("city")
private string city;
@field("postal_code")
private string postalcode;
@field("country")
private string country;
// 地理空间数据
@geospatialindexed(type = geospatialindextype.geo_2dsphere)
@field("location")
private geojsonpoint location;
}
/**
* 审计配置
*/
@configuration
public class auditorconfig implements auditoraware<string> {
@override
public optional<string> getcurrentauditor() {
// 从安全上下文获取当前用户
authentication authentication = securitycontextholder.getcontext().getauthentication();
if (authentication == null || !authentication.isauthenticated()) {
return optional.of("system");
}
return optional.of(authentication.getname());
}
}三、基础crud操作
3.1 插入操作
@service
@slf4j
@transactional
public class userservice {
@autowired
private mongotemplate mongotemplate;
/**
* 插入单个文档
*/
public user insertuser(user user) {
// 设置创建时间
user.setcreatedat(localdatetime.now());
user.setupdatedat(localdatetime.now());
// 插入并返回
user saveduser = mongotemplate.insert(user);
log.info("inserted user: {}", saveduser.getid());
return saveduser;
}
/**
* 批量插入文档
*/
public list<user> batchinsertusers(list<user> users) {
// 设置时间戳
users.foreach(user -> {
user.setcreatedat(localdatetime.now());
user.setupdatedat(localdatetime.now());
});
// 批量插入
collection<user> insertedusers = mongotemplate.insertall(users);
log.info("batch inserted {} users", insertedusers.size());
return new arraylist<>(insertedusers);
}
/**
* 保存或更新(upsert)
*/
public user saveorupdateuser(user user) {
if (user.getid() == null) {
return insertuser(user);
} else {
return updateuser(user);
}
}
/**
* 使用bulkoperations批量操作
*/
public bulkwriteresult bulkinsertusers(list<user> users) {
bulkoperations bulkops = mongotemplate.bulkops(
bulkoperations.bulkmode.ordered, user.class);
list<insertonemodel<user>> operations = users.stream()
.map(user -> {
user.setcreatedat(localdatetime.now());
user.setupdatedat(localdatetime.now());
return new insertonemodel<>(user);
})
.collect(collectors.tolist());
bulkops.insert(users);
return bulkops.execute();
}
}3.2 查询操作
@service
@slf4j
public class userqueryservice {
@autowired
private mongotemplate mongotemplate;
/**
* 基础查询方法
*/
// 根据id查询
public user findbyid(string id) {
return mongotemplate.findbyid(id, user.class);
}
// 查询所有
public list<user> findall() {
return mongotemplate.findall(user.class);
}
// 查询所有(指定集合)
public list<user> findallincollection(string collectionname) {
return mongotemplate.findall(user.class, collectionname);
}
// 条件查询
public list<user> findbyusername(string username) {
query query = query.query(criteria.where("username").is(username));
return mongotemplate.find(query, user.class);
}
// 多条件查询
public list<user> findbycityandagegreaterthan(string city, int minage) {
query query = query.query(
criteria.where("address.city").is(city)
.and("age").gte(minage)
);
return mongotemplate.find(query, user.class);
}
// 模糊查询
public list<user> findbynamelike(string namepattern) {
query query = query.query(
criteria.where("username").regex(namepattern, "i") // 忽略大小写
);
return mongotemplate.find(query, user.class);
}
// in查询
public list<user> findbyids(list<string> ids) {
query query = query.query(criteria.where("id").in(ids));
return mongotemplate.find(query, user.class);
}
// not查询
public list<user> findbycitynot(string city) {
query query = query.query(criteria.where("address.city").ne(city));
return mongotemplate.find(query, user.class);
}
// null查询
public list<user> finduserswithnullemail() {
query query = query.query(criteria.where("email").is(null));
return mongotemplate.find(query, user.class);
}
// exists查询
public list<user> finduserswithaddress() {
query query = query.query(criteria.where("address").exists(true));
return mongotemplate.find(query, user.class);
}
/**
* 分页查询
*/
public page<user> finduserswithpagination(int page, int size, string sortfield) {
query query = new query();
// 总数
long total = mongotemplate.count(query, user.class);
// 分页
query.skip((long) (page - 1) * size)
.limit(size);
// 排序
if (stringutils.isnotblank(sortfield)) {
query.with(sort.by(sort.direction.asc, sortfield));
}
// 查询数据
list<user> content = mongotemplate.find(query, user.class);
return new pageimpl<>(content, pagerequest.of(page - 1, size), total);
}
/**
* 投影查询(只返回指定字段)
*/
public list<user> finduserswithprojection() {
query query = new query();
// 指定返回的字段
query.fields()
.include("username")
.include("email")
.include("address.city")
.exclude("id")
.exclude("createdat")
.exclude("updatedat");
return mongotemplate.find(query, user.class);
}
/**
* 使用distinct去重
*/
public list<string> finddistinctcities() {
return mongotemplate.finddistinct(
new query(), "address.city", user.class, string.class);
}
/**
* 地理空间查询
*/
public list<user> findusersnearlocation(double longitude, double latitude, double maxdistanceinmeters) {
query query = query.query(
criteria.where("address.location")
.near(new point(longitude, latitude))
.maxdistance(maxdistanceinmeters)
);
return mongotemplate.find(query, user.class);
}
/**
* 使用游标处理大数据集
*/
public void processlargeuserdataset() {
query query = query.query(criteria.where("age").gte(18));
try (mongocursor<user> cursor = mongotemplate.stream(query, user.class)) {
while (cursor.hasnext()) {
user user = cursor.next();
// 处理每个用户
processuser(user);
}
}
}
/**
* 查询一个文档
*/
public user findoneuser(string username) {
query query = query.query(criteria.where("username").is(username));
return mongotemplate.findone(query, user.class);
}
/**
* 判断文档是否存在
*/
public boolean exists(string username) {
query query = query.query(criteria.where("username").is(username));
return mongotemplate.exists(query, user.class);
}
/**
* 获取查询数量
*/
public long countusersbycity(string city) {
query query = query.query(criteria.where("address.city").is(city));
return mongotemplate.count(query, user.class);
}
private void processuser(user user) {
// 处理逻辑
}
}3.3 更新操作
@service
@slf4j
@transactional
public class userupdateservice {
@autowired
private mongotemplate mongotemplate;
/**
* 更新整个文档
*/
public user updateuser(user user) {
user.setupdatedat(localdatetime.now());
return mongotemplate.save(user);
}
/**
* 部分更新
*/
public updateresult updateuseremail(string userid, string newemail) {
query query = query.query(criteria.where("id").is(userid));
update update = update.update("email", newemail)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 更新多个文档
*/
public updateresult updateusersagebycity(string city, int newage) {
query query = query.query(criteria.where("address.city").is(city));
update update = update.update("age", newage)
.set("updatedat", localdatetime.now());
return mongotemplate.updatemulti(query, update, user.class);
}
/**
* upsert操作(不存在则插入)
*/
public updateresult upsertuser(user user) {
query query = query.query(criteria.where("username").is(user.getusername()));
update update = new update()
.set("email", user.getemail())
.set("age", user.getage())
.set("updatedat", localdatetime.now())
.setoninsert("createdat", localdatetime.now());
return mongotemplate.upsert(query, update, user.class);
}
/**
* 原子操作:递增字段
*/
public updateresult incrementuserage(string userid, int increment) {
query query = query.query(criteria.where("id").is(userid));
update update = new update().inc("age", increment)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 原子操作:乘除运算
*/
public updateresult multiplyuserscore(string userid, double multiplier) {
query query = query.query(criteria.where("id").is(userid));
update update = new update().multiply("score", multiplier)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 数组操作:向数组添加元素
*/
public updateresult addroletouser(string userid, string role) {
query query = query.query(criteria.where("id").is(userid));
update update = new update().addtoset("roles", role)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 数组操作:从数组移除元素
*/
public updateresult removerolefromuser(string userid, string role) {
query query = query.query(criteria.where("id").is(userid));
update update = new update().pull("roles", role)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 数组操作:向数组添加多个元素
*/
public updateresult addrolestouser(string userid, list<string> roles) {
query query = query.query(criteria.where("id").is(userid));
update update = new update().pushall("roles", roles.toarray())
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 数组操作:更新数组中的特定元素
*/
public updateresult updatearrayelement(string userid, string oldrole, string newrole) {
query query = query.query(criteria.where("id").is(userid)
.and("roles").is(oldrole));
update update = update.update("roles.$", newrole)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 使用findandmodify原子操作
*/
public user findandupdateuser(string userid, update update) {
query query = query.query(criteria.where("id").is(userid));
return mongotemplate.findandmodify(
query,
update,
findandmodifyoptions.options().returnnew(true),
user.class
);
}
/**
* 批量更新操作
*/
public bulkwriteresult bulkupdateusers(list<user> users) {
bulkoperations bulkops = mongotemplate.bulkops(
bulkoperations.bulkmode.ordered, user.class);
for (user user : users) {
query query = query.query(criteria.where("id").is(user.getid()));
update update = update.update("email", user.getemail())
.set("updatedat", localdatetime.now());
bulkops.updateone(query, update);
}
return bulkops.execute();
}
}3.4 删除操作
@service
@slf4j
@transactional
public class userdeleteservice {
@autowired
private mongotemplate mongotemplate;
/**
* 根据id删除
*/
public deleteresult deletebyid(string id) {
query query = query.query(criteria.where("id").is(id));
return mongotemplate.remove(query, user.class);
}
/**
* 根据条件删除
*/
public deleteresult deletebyusername(string username) {
query query = query.query(criteria.where("username").is(username));
return mongotemplate.remove(query, user.class);
}
/**
* 删除所有文档
*/
public deleteresult deleteallusers() {
return mongotemplate.remove(new query(), user.class);
}
/**
* 删除集合(慎用!)
*/
public void dropcollection() {
mongotemplate.dropcollection(user.class);
}
/**
* 软删除
*/
public updateresult softdeleteuser(string userid) {
query query = query.query(criteria.where("id").is(userid));
update update = update.update("isdeleted", true)
.set("updatedat", localdatetime.now());
return mongotemplate.updatefirst(query, update, user.class);
}
/**
* 批量删除
*/
public deleteresult deleteusersbycity(string city) {
query query = query.query(criteria.where("address.city").is(city));
return mongotemplate.remove(query, user.class);
}
/**
* 使用findandremove
*/
public user findandremove(string username) {
query query = query.query(criteria.where("username").is(username));
return mongotemplate.findandremove(query, user.class);
}
/**
* 删除所有软删除的文档
*/
public deleteresult deleteallsoftdeleted() {
query query = query.query(criteria.where("isdeleted").is(true));
return mongotemplate.remove(query, user.class);
}
}四、高级查询与聚合操作
4.1 复杂条件查询
@service
@slf4j
public class advancedqueryservice {
@autowired
private mongotemplate mongotemplate;
/**
* 组合查询条件
*/
public list<user> finduserswithcomplexcriteria(string city, integer minage, integer maxage) {
criteria criteria = new criteria();
// 城市条件
if (stringutils.isnotblank(city)) {
criteria.and("address.city").is(city);
}
// 年龄范围条件
if (minage != null && maxage != null) {
criteria.and("age").gte(minage).lte(maxage);
} else if (minage != null) {
criteria.and("age").gte(minage);
} else if (maxage != null) {
criteria.and("age").lte(maxage);
}
// 邮箱不为空
criteria.and("email").ne(null);
query query = query.query(criteria);
return mongotemplate.find(query, user.class);
}
/**
* or条件查询
*/
public list<user> findusersbycityorage(string city, integer age) {
criteria criteria = new criteria().oroperator(
criteria.where("address.city").is(city),
criteria.where("age").is(age)
);
query query = query.query(criteria);
return mongotemplate.find(query, user.class);
}
/**
* and和or组合查询
*/
public list<user> finduserswithandor() {
criteria criteria = new criteria().andoperator(
criteria.where("age").gte(18).lte(60),
new criteria().oroperator(
criteria.where("address.city").is("beijing"),
criteria.where("address.city").is("shanghai")
)
);
query query = query.query(criteria);
return mongotemplate.find(query, user.class);
}
/**
* 正则表达式查询
*/
public list<user> findusersbyemailpattern(string pattern) {
query query = query.query(
criteria.where("email").regex(pattern, "i") // i表示忽略大小写
);
return mongotemplate.find(query, user.class);
}
/**
* 数组查询:包含所有元素
*/
public list<user> finduserswithallroles(list<string> requiredroles) {
query query = query.query(
criteria.where("roles").all(requiredroles)
);
return mongotemplate.find(query, user.class);
}
/**
* 数组查询:包含任意元素
*/
public list<user> finduserswithanyrole(list<string> roles) {
query query = query.query(
criteria.where("roles").in(roles)
);
return mongotemplate.find(query, user.class);
}
/**
* 数组查询:数组大小
*/
public list<user> finduserswithrolecount(int mincount) {
query query = query.query(
criteria.where("roles").size(mincount)
);
return mongotemplate.find(query, user.class);
}
/**
* 日期范围查询
*/
public list<user> finduserscreatedbetween(localdatetime start, localdatetime end) {
query query = query.query(
criteria.where("createdat").gte(start).lte(end)
);
return mongotemplate.find(query, user.class);
}
/**
* 嵌套文档查询
*/
public list<user> findusersbyaddressdetail(string city, string street) {
query query = query.query(
criteria.where("address.city").is(city)
.and("address.street").regex(street, "i")
);
return mongotemplate.find(query, user.class);
}
/**
* 使用text search(需要创建文本索引)
*/
public list<user> findusersbytext(string searchtext) {
textcriteria criteria = textcriteria.fordefaultlanguage()
.matching(searchtext);
query query = textquery.querytext(criteria)
.sortbyscore() // 按相关性排序
.with(pagerequest.of(0, 10));
return mongotemplate.find(query, user.class);
}
/**
* 地理空间查询:多边形范围内
*/
public list<user> findusersinpolygon(list<double[]> polygonpoints) {
// polygonpoints格式:[[lon1, lat1], [lon2, lat2], ...]
query query = query.query(
criteria.where("address.location")
.within(new geojsonpolygon(polygonpoints))
);
return mongotemplate.find(query, user.class);
}
}4.2 聚合操作
@service
@slf4j
public class aggregationservice {
@autowired
private mongotemplate mongotemplate;
/**
* 基础聚合:分组统计
*/
public list<document> groupusersbycity() {
aggregation aggregation = aggregation.newaggregation(
aggregation.group("address.city")
.count().as("usercount")
.avg("age").as("avgage")
.sum("age").as("totalage"),
aggregation.project("usercount", "avgage", "totalage")
.and("_id").as("city"),
aggregation.sort(sort.direction.desc, "usercount"),
aggregation.limit(10)
);
aggregationresults<document> results = mongotemplate.aggregate(
aggregation, "users", document.class);
return results.getmappedresults();
}
/**
* 多阶段聚合:匹配、分组、排序
*/
public list<document> aggregateactiveusers() {
aggregation aggregation = aggregation.newaggregation(
// 阶段1:过滤条件
aggregation.match(
criteria.where("isdeleted").is(false)
.and("age").gte(18)
),
// 阶段2:按城市分组
aggregation.group("address.city")
.count().as("activeusercount")
.avg("age").as("averageage"),
// 阶段3:投影
aggregation.project()
.and("_id").as("city")
.and("activeusercount").as("usercount")
.and("averageage").as("avgage")
.andexclude("_id"),
// 阶段4:排序
aggregation.sort(sort.direction.desc, "usercount"),
// 阶段5:分页
aggregation.skip(0l),
aggregation.limit(20)
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
/**
* 聚合操作:展开数组
*/
public list<document> unwinduserroles() {
aggregation aggregation = aggregation.newaggregation(
aggregation.match(criteria.where("roles").exists(true)),
aggregation.unwind("roles"),
aggregation.group("roles")
.count().as("usercount"),
aggregation.sort(sort.direction.desc, "usercount")
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
/**
* 聚合操作:关联查询
*/
public list<document> aggregatewithlookup() {
lookupoperation lookup = lookupoperation.newlookup()
.from("orders") // 关联订单集合
.localfield("id")
.foreignfield("userid")
.as("userorders");
aggregation aggregation = aggregation.newaggregation(
aggregation.match(criteria.where("address.city").is("beijing")),
lookup,
aggregation.unwind("userorders", true), // 保留没有订单的用户
aggregation.group("id")
.first("username").as("username")
.sum("userorders.amount").as("totalspent")
.count().as("ordercount"),
aggregation.match(criteria.where("ordercount").gt(0)),
aggregation.sort(sort.direction.desc, "totalspent")
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
/**
* 聚合操作:添加计算字段
*/
public list<document> addcomputedfield() {
aggregation aggregation = aggregation.newaggregation(
aggregation.project()
.and("username").as("username")
.and("age").as("age")
.andexpression("year(createdat)").as("creationyear")
.andexpression("concat(address.city, ', ', address.country)").as("fulladdress")
.andexpression("case when age >= 18 then 'adult' else 'minor' end").as("agegroup"),
aggregation.sort(sort.direction.asc, "creationyear")
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
/**
* 聚合操作:分桶
*/
public list<document> bucketusersbyage() {
aggregation aggregation = aggregation.newaggregation(
aggregation.bucket("age")
.withboundaries(0, 18, 30, 50, 100)
.withdefaultbucket("unknown")
.andoutputcount().as("count")
.andoutput("username").push().as("users")
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
/**
* 聚合操作:抽样
*/
public list<user> sampleusers(int samplesize) {
aggregation aggregation = aggregation.newaggregation(
aggregation.sample(samplesize)
);
return mongotemplate.aggregate(aggregation, "users", user.class)
.getmappedresults();
}
/**
* 聚合操作:图形查找
*/
public list<document> graphlookup() {
aggregation aggregation = aggregation.newaggregation(
aggregation.match(criteria.where("username").is("admin")),
aggregation.graphlookup("users")
.startwith("$reportsto")
.connectfrom("reportsto")
.connectto("id")
.as("managerschain")
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
}4.3 事务管理
@service
@slf4j
@transactional
public class transactionalservice {
@autowired
private mongotemplate mongotemplate;
/**
* 声明式事务
*/
@transactional(rollbackfor = exception.class)
public void transferpoints(string fromuserid, string touserid, int points) {
// 减少转出用户的积分
query query1 = query.query(criteria.where("id").is(fromuserid));
update update1 = update.update("points", points).inc("points", -points);
updateresult result1 = mongotemplate.updatefirst(query1, update1, user.class);
if (result1.getmodifiedcount() == 0) {
throw new runtimeexception("转出用户不存在或积分不足");
}
// 增加转入用户的积分
query query2 = query.query(criteria.where("id").is(touserid));
update update2 = update.update("points", points).inc("points", points);
updateresult result2 = mongotemplate.updatefirst(query2, update2, user.class);
if (result2.getmodifiedcount() == 0) {
throw new runtimeexception("转入用户不存在");
}
// 记录交易日志
transactionlog log = transactionlog.builder()
.fromuserid(fromuserid)
.touserid(touserid)
.points(points)
.timestamp(localdatetime.now())
.build();
mongotemplate.insert(log);
}
/**
* 编程式事务
*/
public void transferpointswithprogrammatictransaction(string fromuserid, string touserid, int points) {
transactiontemplate transactiontemplate = new transactiontemplate(
new mongotransactionmanager(mongotemplate.getmongodatabasefactory())
);
transactiontemplate.execute(status -> {
try {
// 执行事务操作
query query1 = query.query(criteria.where("id").is(fromuserid));
update update1 = update.update("points", points).inc("points", -points);
mongotemplate.updatefirst(query1, update1, user.class);
query query2 = query.query(criteria.where("id").is(touserid));
update update2 = update.update("points", points).inc("points", points);
mongotemplate.updatefirst(query2, update2, user.class);
// 记录日志
transactionlog log = transactionlog.builder()
.fromuserid(fromuserid)
.touserid(touserid)
.points(points)
.timestamp(localdatetime.now())
.build();
mongotemplate.insert(log);
return true;
} catch (exception e) {
status.setrollbackonly();
throw new runtimeexception("转账失败", e);
}
});
}
/**
* 多文档事务
*/
@transactional
public void multidocumenttransaction() {
// 操作1:更新用户
user user = new user();
user.setid("user1");
user.setusername("updated");
mongotemplate.save(user);
// 操作2:插入日志
auditlog auditlog = new auditlog();
auditlog.setaction("update_user");
auditlog.setuserid("user1");
mongotemplate.insert(auditlog);
// 操作3:更新计数器
query query = query.query(criteria.where("name").is("user_update_counter"));
update update = update.update("count", 1).inc("count", 1);
mongotemplate.upsert(query, update, counter.class);
// 如果发生异常,所有操作都会回滚
}
}五、gridfs操作
@service
@slf4j
public class gridfsservice {
@autowired
private mongotemplate mongotemplate;
/**
* 存储文件到gridfs
*/
public string storefile(string filename, inputstream inputstream, string contenttype) {
gridfstemplate gridfstemplate = new gridfstemplate(
mongotemplate.getmongodatabasefactory(),
mongotemplate.getconverter()
);
// 设置元数据
objectmetadata metadata = new objectmetadata();
metadata.put("uploadedby", "system");
metadata.put("uploadtime", localdatetime.now().tostring());
// 存储文件
gridfsfile file = gridfstemplate.store(
inputstream,
filename,
contenttype,
metadata
);
return file.getobjectid().tostring();
}
/**
* 从gridfs读取文件
*/
public gridfsresource getfile(string fileid) {
gridfstemplate gridfstemplate = new gridfstemplate(
mongotemplate.getmongodatabasefactory(),
mongotemplate.getconverter()
);
gridfsfile file = gridfstemplate.findone(
query.query(criteria.where("_id").is(new objectid(fileid)))
);
if (file == null) {
throw new runtimeexception("file not found");
}
return gridfstemplate.getresource(file);
}
/**
* 删除gridfs文件
*/
public void deletefile(string fileid) {
gridfstemplate gridfstemplate = new gridfstemplate(
mongotemplate.getmongodatabasefactory(),
mongotemplate.getconverter()
);
gridfstemplate.delete(query.query(criteria.where("_id").is(new objectid(fileid))));
}
/**
* 查询gridfs文件
*/
public list<gridfsfile> findfilesbymetadata(string key, string value) {
gridfstemplate gridfstemplate = new gridfstemplate(
mongotemplate.getmongodatabasefactory(),
mongotemplate.getconverter()
);
return gridfstemplate.find(
query.query(criteria.where("metadata." + key).is(value))
);
}
}六、性能优化与监控
6.1 索引管理
@service
@slf4j
public class indexmanagementservice {
@autowired
private mongotemplate mongotemplate;
/**
* 创建索引
*/
public void createindexes() {
indexoperations indexops = mongotemplate.indexops(user.class);
// 创建单字段索引
indexops.ensureindex(new index().on("username", sort.direction.asc));
// 创建唯一索引
indexops.ensureindex(new index().on("email", sort.direction.asc).unique());
// 创建复合索引
indexops.ensureindex(
new index().on("address.city", sort.direction.asc)
.on("age", sort.direction.desc)
.named("city_age_idx")
);
// 创建ttl索引(自动过期)
indexops.ensureindex(
new index().on("createdat", sort.direction.asc)
.expire(30, timeunit.days) // 30天后自动删除
);
// 创建文本索引
indexops.ensureindex(
new index().on("username", sort.direction.asc)
.on("email", sort.direction.asc)
.named("text_search_idx")
);
// 创建地理空间索引
indexops.ensureindex(
new index().on("address.location", indexdirection.geo_2dsphere)
);
// 创建部分索引
indexops.ensureindex(
new index().on("age", sort.direction.asc)
.partial(filter.filter(criteria.where("age").gte(18)))
);
}
/**
* 获取所有索引
*/
public list<document> getallindexes() {
indexoperations indexops = mongotemplate.indexops(user.class);
return indexops.getindexinfo();
}
/**
* 删除索引
*/
public void dropindex(string indexname) {
indexoperations indexops = mongotemplate.indexops(user.class);
indexops.dropindex(indexname);
}
/**
* 重建索引
*/
public void rebuildindexes() {
indexoperations indexops = mongotemplate.indexops(user.class);
indexops.dropallindexes();
createindexes();
}
/**
* 检查索引是否存在
*/
public boolean indexexists(string indexname) {
indexoperations indexops = mongotemplate.indexops(user.class);
return indexops.getindexinfo().stream()
.anymatch(index -> index.getname().equals(indexname));
}
}6.2 性能监控
@aspect
@component
@slf4j
public class mongoperformancemonitor {
private final meterregistry meterregistry;
public mongoperformancemonitor(meterregistry meterregistry) {
this.meterregistry = meterregistry;
}
@around("execution(* org.springframework.data.mongodb.core.mongotemplate.*(..))")
public object monitormongooperations(proceedingjoinpoint joinpoint) throws throwable {
string operationname = joinpoint.getsignature().getname();
long starttime = system.currenttimemillis();
try {
object result = joinpoint.proceed();
long duration = system.currenttimemillis() - starttime;
// 记录指标
meterregistry.timer("mongodb.operations", "operation", operationname)
.record(duration, timeunit.milliseconds);
// 记录慢查询
if (duration > 1000) { // 超过1秒
log.warn("slow mongodb operation detected: {} took {}ms",
operationname, duration);
if (log.isdebugenabled()) {
object[] args = joinpoint.getargs();
log.debug("operation arguments: {}", arrays.tostring(args));
}
}
return result;
} catch (exception e) {
// 记录错误指标
meterregistry.counter("mongodb.errors", "operation", operationname)
.increment();
throw e;
}
}
}
/**
* 健康检查配置
*/
@component
public class mongohealthindicator implements healthindicator {
@autowired
private mongotemplate mongotemplate;
@override
public health health() {
try {
// 执行简单查询检查连接
document stats = mongotemplate.executecommand("{ serverstatus: 1 }");
return health.up()
.withdetail("version", stats.get("version"))
.withdetail("host", mongotemplate.getdb().getname())
.withdetail("collections", mongotemplate.getcollectionnames())
.build();
} catch (exception e) {
return health.down()
.withexception(e)
.build();
}
}
}6.3 查询优化
@service
@slf4j
public class queryoptimizationservice {
@autowired
private mongotemplate mongotemplate;
/**
* 使用投影优化查询
*/
public list<user> optimizedfindusers() {
query query = new query();
// 只返回需要的字段
query.fields()
.include("username")
.include("email")
.exclude("_id");
// 限制返回数量
query.limit(100);
// 使用索引覆盖查询
query.withhint("username_1_email_1");
return mongotemplate.find(query, user.class);
}
/**
* 批量操作优化
*/
public void batchoptimizedoperations() {
// 使用bulkoperations进行批量操作
bulkoperations bulkops = mongotemplate.bulkops(
bulkoperations.bulkmode.unordered, user.class);
// 批量插入
list<user> userstoinsert = generateusers(1000);
bulkops.insert(userstoinsert);
// 批量更新
query updatequery = query.query(criteria.where("age").lt(18));
update update = update.update("category", "minor");
bulkops.updatemulti(updatequery, update);
// 执行批量操作
bulkwriteresult result = bulkops.execute();
log.info("bulk operation result: {}", result);
}
/**
* 使用游标处理大数据集
*/
public void processlargedatasetwithcursor() {
query query = query.query(criteria.where("createdat")
.gte(localdatetime.now().minusmonths(1)));
// 设置游标选项
query.cursorbatchsize(1000); // 每批获取1000条
try (mongocursor<user> cursor = mongotemplate.stream(query, user.class)) {
int count = 0;
while (cursor.hasnext()) {
user user = cursor.next();
processuser(user);
count++;
// 每处理1000条记录一次日志
if (count % 1000 == 0) {
log.info("processed {} records", count);
}
}
}
}
/**
* 使用聚合管道优化复杂查询
*/
public list<document> optimizedaggregation() {
aggregation aggregation = aggregation.newaggregation(
// 尽早过滤数据
aggregation.match(criteria.where("isactive").is(true)),
// 使用索引字段进行排序
aggregation.sort(sort.direction.desc, "createdat"),
// 限制数据量
aggregation.limit(1000),
// 只投影需要的字段
aggregation.project()
.and("username").as("name")
.and("email").as("contact")
.andexclude("_id"),
// 最后进行复杂计算
aggregation.addfields()
.addfield("namelength").withvalueof(stringoperators.strlencp("$name"))
.build()
);
return mongotemplate.aggregate(aggregation, "users", document.class)
.getmappedresults();
}
private list<user> generateusers(int count) {
list<user> users = new arraylist<>();
for (int i = 0; i < count; i++) {
user user = new user();
user.setusername("user" + i);
user.setemail("user" + i + "@example.com");
user.setage(new random().nextint(50) + 18);
users.add(user);
}
return users;
}
private void processuser(user user) {
// 处理逻辑
}
}七、最佳实践总结
7.1 设计原则
- 合理使用索引:为查询频繁的字段创建索引,注意索引顺序
- 避免大文档:单个文档不应超过16mb,考虑拆分大文档
- 选择合适的数据模型:根据查询模式选择内嵌或引用
- 使用合适的字段类型:选择最合适的bson类型存储数据
7.2 性能优化
- 查询优化:
- 使用投影减少返回字段
- 合理使用分页和限制
- 避免全表扫描
- 写入优化:
- 使用批量操作
- 合理使用索引(避免过多索引影响写入性能)
- 考虑写入确认级别
- 连接池优化:
- 根据并发量调整连接池大小
- 监控连接使用情况
- 合理设置超时时间
7.3 监控告警
- 健康检查:定期检查mongodb连接状态
- 性能监控:监控查询响应时间、错误率
- 资源监控:监控cpu、内存、磁盘使用情况
- 慢查询日志:记录和分析慢查询
7.4 安全实践
- 认证授权:使用用户名密码认证
- 网络隔离:在内网环境中运行
- 加密传输:启用tls/ssl加密
- 定期备份:制定备份和恢复策略
八、完整示例项目
8.1 项目结构
src/main/java/com/example/mongodemo/ ├── config/ │ ├── mongoconfig.java │ ├── mongoauditconfig.java │ └── multimongoconfig.java ├── entity/ │ ├── baseentity.java │ ├── user.java │ ├── address.java │ └── auditlog.java ├── repository/ │ ├── userrepository.java │ └── customuserrepository.java ├── service/ │ ├── userservice.java │ ├── userqueryservice.java │ ├── aggregationservice.java │ └── transactionservice.java ├── controller/ │ └── usercontroller.java └── mongodemoapplication.java
8.2 启动类配置
@springbootapplication
@enablemongorepositories
@enabletransactionmanagement
@enablecaching
@slf4j
public class mongodemoapplication {
public static void main(string[] args) {
springapplication.run(mongodemoapplication.class, args);
}
@bean
public commandlinerunner initdata(userservice userservice) {
return args -> {
log.info("initializing sample data...");
// 创建测试用户
user user = user.builder()
.username("testuser")
.email("test@example.com")
.age(25)
.address(address.builder()
.city("beijing")
.country("china")
.build())
.build();
userservice.insertuser(user);
log.info("sample user created: {}", user.getusername());
};
}
}
九、总结
通过本文的全面介绍,您应该已经掌握了spring boot中mongotemplate的核心使用方法。关键点包括:
- 配置灵活:支持单机、副本集、分片集群等多种部署方式
- 操作全面:涵盖crud、聚合、事务、gridfs等所有操作
- 性能优秀:提供丰富的性能优化选项和监控手段
- 易于集成:与spring生态系统无缝集成
在实际项目中,建议根据具体业务需求选择合适的操作方式,并持续监控和优化数据库性能。
到此这篇关于spring boot中mongotemplate深度指南:从入门到实战的文章就介绍到这了,更多相关springboot mongotemplate使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论