Spring MongoRepository的saveAll()是否在一个批量中插入数据?

Spring MongoRepository的saveAll()是否在一个批量中插入数据?,spring,mongodb,spring-boot,Spring,Mongodb,Spring Boot,我希望使保存操作更高效,因此我希望每隔一段时间向Mongo写入大量对象(即当超过某些容量时) saveAll()能帮我吗?我应该改用BulkOperations吗?简短回答,是,但前提是所有文档都是新文档。如果没有,它将逐个插入或更新 看看SimpleMongoRepository(MongoRepository的默认实现): public List saveAll(可编辑实体){ Assert.notNull(entities,“给定的Iterable of entities不能为null!”

我希望使保存操作更高效,因此我希望每隔一段时间向Mongo写入大量对象(即当超过某些容量时)


saveAll()
能帮我吗?我应该改用
BulkOperations
吗?

简短回答,是,但前提是所有文档都是新文档。如果没有,它将逐个插入或更新

看看SimpleMongoRepository(MongoRepository的默认实现):

public List saveAll(可编辑实体){
Assert.notNull(entities,“给定的Iterable of entities不能为null!”);
可流化源=可流化的(实体);
布尔allNew=source.stream().allMatch((it)->{
返回此.entityInformation.isNew(it);
});
如果(全新){
列表结果=(列表)source.stream().collect(Collectors.toList());
返回新的ArrayList(this.mongoOperations.insert(result,this.entityInformation.getCollectionName());
}否则{
return(List)source.stream().map(this::save.collect(Collectors.toList());
}
}
请注意,当所有文档都是新文档时,存储库将使用MongoOperations.insert方法(MongoTemplate是实现),然后,如果查看该方法的代码,您会发现它执行了批量插入:

public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName) {
        Assert.notNull(batchToSave, "BatchToSave must not be null!");
        Assert.notNull(collectionName, "CollectionName must not be null!");
        return this.doInsertBatch(collectionName, batchToSave, this.mongoConverter);
    }

public Collection insert(Collection)那么如何进行批量更新?
public <T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName) {
        Assert.notNull(batchToSave, "BatchToSave must not be null!");
        Assert.notNull(collectionName, "CollectionName must not be null!");
        return this.doInsertBatch(collectionName, batchToSave, this.mongoConverter);
    }