MongoDB with Spring Data如何有条件地向上插入?

问题描述 投票:0回答:1

当前正在尝试使用Spring数据在MongoDB上实现更新操作。

公立班级学生{

    @Indexed(unique = true)
    @NotNull
    private String studentId;
    private Long triggerTime;
    ...

}

我实际上想要实现的是条件更新。 (如果存在,则更新/如果不基于studentId,则创建(如果不基于studentId,则创建)),只要所保存的学生文档的triggerTime较长。

阅读有关使用WriteConcern.ACKNOWLEDGED配置WriteConcernResolver的内容,如下所示:

public WriteConcern resolve(MongoAction action) {

        if (action.getCollectionName().getClass().getSimpleName().contains("Student")) {
            return WriteConcern.ACKNOWLEDGED;
        }
        return action.getDefaultWriteConcern();
    }

但是我不了解MongoAction对象以及如何创建它。还是我完全错了??

当前正在尝试使用Spring数据在MongoDB上实现更新操作。公共类Student {@Indexed(unique = true)@NotNull私有字符串studentId;私人Long ...

mongodb spring-data-mongodb
1个回答
0
投票

我不确定这是否有帮助,但是在我看来,Mongo Update查询(https://docs.mongodb.com/manual/reference/method/db.collection.update/)允许使用“查询”参数,这将影响它所作用的数据。如果您编写一个语义为other.studentId NOT EXISTS OR (other.studentId EXISTS AND other.triggerTime < this.triggerTime)的查询并将该查询传递给Mongo upsert操作,那应该可以解决您的问题,对吧?

© www.soinside.com 2019 - 2024. All rights reserved.