0

1) Problem Description:

  • I am using a Reactive Spring-Data + Spring WebFlux + MongoDb + ReactiveMongoTemplate;
  • I have 02 Collections: Task and Project

2) My Goal is:

  • Using only “one update method”
  • that updates the 02 documents inside the 02 Collections (at the same time - simultaneously)

3) Code:

3.1) Collection Task

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

  @Id
  private String _id;

  @Field("pid")
  private String projectId;

  private String taskName;

  private String ownerProject;
}

3.2) Collection Project

@Document(collection = "project")
public class Project {
  @Id
  private String _id;

  private String ownerProject;

  private Task tasksProject;

  private String code;
}

3.3) My current code - which is not working

 public Mono<Void> updateWithCriteriaTemplateMult(String id) {

    Query projectDocument = new Query();
    projectDocument.addCriteria(Criteria.where("_id")
                                             .is(id));

    Query taskDocument = new Query();
    taskDocument.addCriteria(Criteria.where("projectId")
                                          .is(id));


    return template
         .update(projectDocument, Project.class)
         .then(template.update(taskDocument, Task.class))
         ;
  }

2) Question:

  • How Can I code/do “the same UPDATE-METHOD” updates both Collections “at the same time” using ReactiveMongoTemplate?

    • Example 01: My method needs to “update” the field “ownerProject” in both Collections:
      • Task: in this case ‘ownerProject’ is a simple field
      • Project: in this case ‘ownerProject’ is a simple field
  • Example 02:

    • My method needs to “update” the field “taskName” in both Collections:
      • Task: in this case ‘TaskName’ is a simple field
      • Project: in this case ‘TaskName’ is field “inside” “tasksProject”

Thanks a lot for any help

GtdDev
  • 748
  • 6
  • 14

1 Answers1

0

Update

Thats my solution for this problem, using delete instead update (same idea):

    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));
  }

For better understanding, follow There are my entities:

@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;
}

GtdDev
  • 748
  • 6
  • 14