如何在mongo中更新文档以获得性能? [英] How to update document in mongo to get performance?

查看:69
本文介绍了如何在mongo中更新文档以获得性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Data Mongo 的新手.我有一个场景,如果mongo db中尚不存在,我想创建一个Study.如果已经存在,则必须使用新值对其进行更新.

I am new to Spring Data Mongo. I've a scenario where I want to create a Study if already not present in mongo db. If its already present, then I've to update it with the new values.

我按照以下方式尝试过,在我的情况下效果很好,但是就性能而言,我不确定这是正确的/最佳/明智的更新方式.

I tried in the following way, which works fine in my case, but I'm not sure this is the correct/Best/Advisable way to update etc as far as performance is concerned.

有人可以对此进行指导吗?

Could anyone please guide on this?

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);
        }
    }

推荐答案

您将必须使用MongoTemplate.upsert()来实现此目的. 您将需要再添加两个类:StudyRepositoryCustom(是interface)和扩展此接口的类,例如StudyRepositoryImpl

You will have to use the MongoTemplate.upsert() to achieve this. You will need to add two more classes: StudyRepositoryCustom which is an interface and a class that extends this interface, say StudyRepositoryImpl

interface StudyRepositoryCustom {
   public WriteResult updateStudy(Study study);
}

将此interface

@Repository
public interface StudyRepository extends MongoRepository<Study, String>, StudyRepositoryCustom {
   // ... Your code as before
}

并添加一个实现StudyRepositoryCustom的类.这是我们@AutowireMongoTemplate的地方,并提供用于更新Study或保存(如果不存在)的实现.我们使用MongoTemplate.upsert()方法.

And add a class that implements the StudyRepositoryCustom. This is where we will @Autowire our MongoTemplate and provide the implementation for updating a Study or saving it if it does not exist. We use the MongoTemplate.upsert() method.

class StudyRepositoryImpl implements StudyRepositoryCustom {
   @Autowired
   MongoTemplate mongoTemplate;

   public WriteResult updateStudy(Study study) {
      Query searchQuery = new Query(Criteria.where("id").is(study.getId());
      WriteResult update = mongoTemplate.upsert(searchQuery, Update.update("title", study.getTitle).set("description", study.getDescription()).set(...)), Study.class);
      return update;
   }
}

请注意,StudyRepositoryImpl将由Spring Data基础结构自动拾取,因为我们遵循了以Impl

Kindly note that StudyRepositoryImpl will automatically be picked up by the Spring Data infrastructure as we've followed the naming convention of extending the core repository interface's name with Impl

检查

Check this example on github, for @Autowire-ing a MongoTemplate and using custom repository as above.

我尚未测试代码,但它会指导您:-)

这篇关于如何在mongo中更新文档以获得性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆