0
4

1 回答 1

0

更新

这就是我对这个问题的解决方案,使用删除而不是更新(同样的想法):

    public Mono<ProjectChild> DeleteCritTemplMultCollections(
       String projectId,
       String taskId) {

    Query project = Query.query(Criteria.where("_id")
                                        .in(projectId));

    Query taskToDelete = Query.query(Criteria.where("_id")
                                             .in(taskId));

    Update deleteTaskInProjectCollection = new Update();
    deleteTaskInProjectCollection.pull("tasks", taskToDelete);

    Mono<DeleteResult> removeTaskInTaskCollection =
         template.remove(taskToDelete, Task.class);

    return template
         .updateMulti(project, deleteTaskInProjectCollection, "projectchild")
         .then(removeTaskInTaskCollection)
         .then(template.findById(projectId, ProjectChild.class));
  }

为了更好地理解,请遵循我的实体:

@Getter
@Setter
@Document(collection = "projectchild")
public class ProjectChild {
  @Id
  private String _id;

  private String name;

  private String code;

  @Field("desc")
  private String description;

  private String startDate;

  private String endDate;

  @Field("cost")
  private long estimatedCost;

  private List<String> countryList;

  private List<Task> tasks;

  @Version
  private Long version;
}


@Getter
@Setter
@Document(collection = "task")
public class Task {

  @Id
  private String _id;

  @Field("pid")
  private String projectId;

  private String name;

  @Field("desc")
  private String description;

  private String ownername;

  private long cost;

  @Version
  private Long version;
}

于 2021-12-23T16:19:33.490 回答