spring 数据 mongo 数据库存储库保存

spring data mongo db repository save

是 MongoRepository 的保存方法确实一个一个地保存文档或者它使用 mongo 的批量插入功能。我面临的问题是,我将 1000 个文档的 ist 传递到存储库,但性能上下文没有任何变化。

Interface MongoRepository<T,ID extends Serializable>

save

<S extends T> List<S> save(Iterable<S> entites)

Specified by:
    save in interface CrudRepository<T,ID extends Serializable>

不确定它的超级 class crudrespository 在做什么,因为它是特定于 JPA 而不是 mongo

它使用批量插入。以 MongoRepository 实现为例 - SimpleMongoRepository#save method. In case of insertion, it will invoke insertAll from MongoOperations and implementation of MongoOperations is MongoTemplate. And now you can take a look at MongoTemplate#insertAll and you will see it invokes MongoTemplate#doInsertAll which performs bulk insert by calling MongoTemplate#doInsertBatch 方法。

是的。插入 1000 个文档可能不会显着反映性能差异。