spring-data-mongodb的批量更新

本文介绍使用Spring Data MongoDB 2.0.7进行批量更新的方法,包括如何组装UpdateManyModel及执行更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近公司在做项目的时候用到mongo数据库 因为数据量比较大,所以需要优化的地方比较多,其中批量更新花了我很多的时间 特记录一下 备忘!

这里我用到spring-data-mongodb的版本是 2.0.7 
spring的版本是5.0.7  这里需要注意下 spring 5才能和spring-data-mongodb 2兼容 

还有个需要注意的地方是 spring5和log4j的2版本兼容 和1已经不兼容了

spring-data-mongodb 1版本在网上有很多的批量更新方法 我这里就不赘述了
 

spring-data-mongdb 1版本的批量更新

废话不多说 上代码

首先获得组装List<UpdateManyModel<Document>>

@Override
public int batchUpdate(List<FrankPo> frankPos) throws IllegalAccessException {
    UpdateOptions uo = new UpdateOptions();
    uo.upsert(true);
    List<UpdateManyModel<Document>> updates = new ArrayList<>();
    for (FrankPo frankPo : frankPos) {
        // 查询的条件 根据此条件查询需要更新的记录 比如{"id":1}
        Document query = new Document(MAC ,frankPo.getMac());
        Map<String, Object> objectMap = ObjectReflectionUtil.getObjectMap(frankPo);
        // 更新语句 比如:{"name":"123"}
        Document update = new Document();
        for (Map.Entry<String, Object> entry : objectMap.entrySet()) {
            update.put(entry.getKey(),entry.getValue());
        }
        // 这里update需要再次包装 包装后形成语句{"$set":{"name":"123"}}   SystemConstant.MONGO_SET = "$set"
        updates.add(new UpdateManyModel<Document>(query,new Document(SystemConstant.MONGO_SET,update),uo));
    }
    return super.batchUpdates(updates);
}

 

然后去dao执行sql

protected int batchUpdates(List<UpdateManyModel<Document>> updates){
        logger.info("batchUpdates 参数--->" + JSONArray.toJSONString(updates) + " 集合名-->" + getCollectionName());
        // 获得对应数据库的连接 getCollectionName()方法获取的是数据库中的集合名
        MongoCollection<Document> collection = mongoTemplate.getCollection(getCollectionName());
        // 这里返回的是被更新的记录的个数
        return collection.bulkWrite(updates).getModifiedCount();
    }

 

至此,批量更新也就结束了 其实这也就是我们自己去组装sql执行的方式

转载请注明出处!

Spring Data MongoDB 中,`MongoWriter` 接口用于将对象转换为 MongoDB 文档并插入到集合中。`doInsertBatch` 方法是 `MongoTemplate` 类中的一个方法,用于批量插入数据。 下面是一个简单的示例,展示了如何使用 `MongoWriter` 和 `doInsertBatch` 方法将对象批量插入到 MongoDB 集合中: ```java public class Person { private String name; private int age; // getters and setters } public class PersonWriter implements MongoWriter<Person> { @Override public Document toDocument(Person object, MongoConverter converter) { Document document = new Document(); document.put("name", object.getName()); document.put("age", object.getAge()); return document; } } public class PersonDao { private MongoTemplate mongoTemplate; public void insertBatch(List<Person> persons) { mongoTemplate.execute(Person.class, collection -> { List<Document> documents = new ArrayList<>(); for (Person person : persons) { MongoWriter<Person> writer = new PersonWriter(); Document document = writer.toDocument(person, mongoTemplate.getConverter()); documents.add(document); } collection.insertMany(documents); return null; }); } } ``` 在上面的示例中,`PersonWriter` 类实现了 `MongoWriter` 接口,并重写了 `toDocument` 方法,将 `Person` 对象转换为 MongoDB 文档。`PersonDao` 类中的 `insertBatch` 方法使用 `MongoTemplate` 的 `execute` 方法执行 MongoDB 操作。在该方法内部,首先将传入的 `Person` 对象列表转换为 MongoDB 文档,并将这些文档存储在 `documents` 列表中。最后,使用 `collection.insertMany` 方法将文档批量插入到 MongoDB 集合中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值