【问题标题】:Spring MongoTemplate upsert entire objectSpring MongoTemplate upsert 整个对象
【发布时间】:2016-11-05 17:09:32
【问题描述】:

对于 Upsert MongoTemplate 提供了一种方法,可以按以下方式使用。

但是,它只会设置名称。如果我想插入与新用户对象关联的所有键怎么办。即除了名称之外,我还想插入电子邮件和许多其他值。 我可以为每个项目使用set("key","name"),但是否可以保存整个对象以供更新插入。

Query query = new Query();
query.addCriteria(Criteria.where("name").is("Markus"));
Update update = new Update();
update.set("name", "Nick");
mongoTemplate.upsert(query, update, User.class);

【问题讨论】:

标签: spring upsert mongotemplate


【解决方案1】:

我猜save 可以为你做到这一点。这是documentation

【讨论】:

    【解决方案2】:

    mongoTemplate.save(user); or mongoTemplate.save(user,"customer");//if you need to save it in another collection name.

    保存将执行 如果对象尚不存在则插入,否则它将更新, 这是一个“更新”。

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2020-09-28
      相关资源
      最近更新 更多