Mongodb 如何在mongo中更新文档以获得性能?
我是Spring Data Mongo的新手。我有一个场景,如果mongo db中还没有,我想创建一个研究。如果它已经存在,那么我必须用新值更新它 我尝试了以下方法,在我的情况下效果很好,但就性能而言,我不确定这是正确/最好/可取的更新方法 谁能给我介绍一下吗Mongodb 如何在mongo中更新文档以获得性能?,mongodb,spring-data-mongodb,Mongodb,Spring Data Mongodb,我是Spring Data Mongo的新手。我有一个场景,如果mongo db中还没有,我想创建一个研究。如果它已经存在,那么我必须用新值更新它 我尝试了以下方法,在我的情况下效果很好,但就性能而言,我不确定这是正确/最好/可取的更新方法 谁能给我介绍一下吗 public void saveStudy(List<Study> studies) { for (Study study : studies) { String id = study.
public void saveStudy(List<Study> studies) {
for (Study study : studies) {
String id = study.getId();
Study presentInDBStudy = studyRepository.findOne(id);
//find the document, modify and update it with save() method.
if(presentInDBStudy != null) {
presentInDBStudy.setTitle(task.getTitle());
presentInDBStudy.setDescription(study.getDescription());
presentInDBStudy.setStart(study.getStart());
presentInDBStudy.setEnd(study.getEnd());
repository.save(presentInDBStudy);
}
else
repository.save(study);
}
}
public void保存研究(列表研究){
用于(学习:学习){
String id=study.getId();
研究现状研究=studyRepository.findOne(id);
//找到文档,用save()方法修改并更新它。
如果(PresentIdbStudy!=null){
presentibstudy.setTitle(task.getTitle());
presentDBStudy.setDescription(study.getDescription());
presentibstudy.setStart(study.getStart());
presentibstudy.setEnd(study.getEnd());
repository.save(presentidstudy);
}
其他的
保存(研究);
}
}
如mongo文档中所述,您可以为此使用upsert功能。
您必须使用
MongoTemplate.upsert()
来实现这一点。
您需要再添加两个类:StudyRepositoryCustom
,它是一个接口
和一个扩展此接口的类,比如StudyRepositoryImpl
interface StudyRepositoryCustom {
public WriteResult updateStudy(Study study);
}
将当前的StudyRepository
更新为extend
此界面
@Repository
public interface StudyRepository extends MongoRepository<Study, String>, StudyRepositoryCustom {
// ... Your code as before
}
请注意,StudyRepositoryImpl
将由Spring数据基础架构自动获取,因为我们遵循了使用Impl
扩展核心存储库接口名称的命名约定
interface StudyRepositoryCustom {
public WriteResult updateStudy(Study study);
}
查看github上的示例,查看@Autowire
-ingMongoTemplate
,并如上所述使用自定义存储库
我没有测试代码,但它将指导您:-)您可以更新代码以使用
列表保存(Iterable entites)
保存所有实体。Spring的MongoRepository
将根据_id字段的存在及其值处理所有可能的情况
更多信息请点击此处
这对于基本的保存操作非常有效。您不必加载文档进行更新。只需设置id,并确保通过替换现有文档在更新时包含所有要更新的字段
简化域对象:
@Document(collection = "study")
public class Study {
@Id
private String id;
private String name;
private String value;
}
存储库:
public interface StudyRepository extends MongoRepository<Study, String> {}
运行所有可能的案例:
public void saveStudies() {
List<Study> studies = new ArrayList<Study>();
--Updates the existing record by replacing with the below values.
Study update = new Study();
update.setId(1);
update.setName("saveType");
update.setValue("update");
studies.add(update);
--Inserts a new record.
Study insert = new Study();
insert.setName("saveType");
insert.setValue("insert");
studies.add(insert);
--Upserts a record.
Study upsert = new Study();
upsert.setId(2);
upsert.setName("saveType");
upsert.setValue("upsert");
studies.add(upsert);
studyRepository.save(studies);
}
请给mongo文档参考链接。这并不能回答问题。我想使用spring数据mongo api。请进一步指导
public void saveStudies() {
List<Study> studies = new ArrayList<Study>();
--Updates the existing record by replacing with the below values.
Study update = new Study();
update.setId(1);
update.setName("saveType");
update.setValue("update");
studies.add(update);
--Inserts a new record.
Study insert = new Study();
insert.setName("saveType");
insert.setValue("insert");
studies.add(insert);
--Upserts a record.
Study upsert = new Study();
upsert.setId(2);
upsert.setName("saveType");
upsert.setValue("upsert");
studies.add(upsert);
studyRepository.save(studies);
}
{
"_id" : 1,
"_class" : "com.mongo.Study",
"name" : "saveType",
"value" : "update"
}
{
"_id" : 3,
"_class" : "com.mongo.Study",
"name" : "saveType",
"value" : "insert"
}
{
"_id" : 2,
"_class" : "com.mongo.Study",
"name" : "saveType",
"value" : "upsert"
}