spring - 在 MongoDB for Spring 中查找/删除 @DBref 列表项

标签 spring mongodb spring-mvc spring-boot mongodb-query

在 MongoDB for Spring 中查找和更新 @DBRef 嵌套列表项的最佳方法是什么?

我有一个AppliedApplication类:

@Document(collection = "applied_application")
public class AppliedApplication {

    @Id
    private String id;
    @Field("program")
    @DBRef
    private List<Program> programList;

    // getters and setters

}

使用 Program 类作为 @DBRef:

@Document(collection = "program")
    public class Program {

        @Id
        private String id;
        @Field("program_name")
        private String programName;

        // getters and setters

    }

我正在寻找一种使用以下查询查找和更新嵌套列表项的方法:

用于通过 AppliedApplication 集合中的 Id 查找 Program 对象的查询:

Query query = new Query();
     query.addCriteria(Criteria.where("id").is(applicationId)
      .and("program.$id").is(new ObjectId(programId)));

Program program = mongoTemplate.findOne(query, Program.class);

用于从 AppliedApplication 中删除列表项的查询:

Update update = new Update().pull("program", new BasicDBObject("program.$id", new ObjectId(programId)));

mongoTemplate.updateMulti(new Query(), update, AppliedApplication.class);

它们都不起作用,我完全一无所知。

最佳答案

查找:

使用位置运算符/$elemMatch在AppliedApplication中查找匹配的程序DBRef。

使用$positional投影

Query query = new Query();
query.addCriteria(Criteria.where("id").is(new ObjectId(applicationId)).and("program.$id").is(new ObjectId(programId)));
query.fields().position("program", 1);
AppliedApplication application = mongoTemplate.findOne(query, AppliedApplication.class);
Program program = application.getProgramList().get(0);

使用$elemMatch投影

Query query = new Query();
query.addCriteria(Criteria.where("id").is(new ObjectId(applicationId)));
query.fields().elemMatch("program", Criteria.where("$id").is(new ObjectId(programId)));
AppliedApplication application = mongoTemplate.findOne(query, AppliedApplication.class);
Program program = application.getProgramList().get(0);

删除:

使用$pull从程序DBref列表中删除DBRef。

Query query = Query.query(Criteria.where("$id").is(new ObjectId(programId)));
Update update = new Update().pull("program", query);
mongoTemplate.updateMulti(new Query(), update, AppliedApplication.class);

添加

使用$push将新程序添加到列表中。

Query query = new Query();
query.addCriteria(Criteria.where("id").is(new ObjectId(applicationId)));
Update update = new Update().push("program", new DBRef("program", new ObjectId(programId));
mongoTemplate.updateMulti(query, update, AppliedApplication.class);

关于spring - 在 MongoDB for Spring 中查找/删除 @DBref 列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948638/

相关文章:

javascript - 如何在 NODEJS 中使用 Angular Ajax 调用从 Request POST 对象获取对象?

spring-mvc - 如何使用@ControllerAdvice @ModelAttribute 定位特定的处理程序?

java - 无法使用 Geronimo 检索 "entityManagerFactory"

hibernate - 在 Spring/Hibernate 堆栈中打开 session 的位置?

java - 在运行时按需创建模式和表

javascript - 如何在express.js中编写查找和更新查询?

javascript - Mongoose 一对多,如何获取类别的字段值

java - 如何使用 class + spring 4.0.0 配置 websocket 句柄

java - 为什么在SpringMVC中rest-servlet,xml是必须的?

spring - 当与 Apache Kafka Server 的连接丢失时,如何使用 Spring Kafka Listener 停止微服务?