弹簧靴&x2B;MongoDB:在一个查询中更新并获取大小

弹簧靴&x2B;MongoDB:在一个查询中更新并获取大小,mongodb,spring-boot,Mongodb,Spring Boot,我使用的是SpringBoot2.1.3.RELEASE和MongoDB。我需要更新文档中的列表,并在一个查询中获取该列表的计数 文档示例: @Data public class Company { @Id private String id; private List<String> departments; } 计数查询: final Aggregation aggregation = newAggregation( ma

我使用的是SpringBoot2.1.3.RELEASE和MongoDB。我需要更新文档中的列表,并在一个查询中获取该列表的计数

文档示例:

  @Data
  public class Company {
    @Id
    private String id;

    private List<String> departments; 
  }
计数查询:

final Aggregation aggregation = newAggregation(
        match(where("_id").is("companyId")),
        project()
            .and("departments")
            .size()
            .as("count")
    );
它必须是原子的。有没有办法做到这一点

final Aggregation aggregation = newAggregation(
        match(where("_id").is("companyId")),
        project()
            .and("departments")
            .size()
            .as("count")
    );