Skip to content

Document BulkOperations limitations #4082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sriramkishoren opened this issue Jun 12, 2022 · 1 comment
Closed

Document BulkOperations limitations #4082

sriramkishoren opened this issue Jun 12, 2022 · 1 comment
Assignees
Labels
type: documentation A documentation update

Comments

@sriramkishoren
Copy link

sriramkishoren commented Jun 12, 2022

Annotation @version is not working in mongodb BulkOperations insert operation but working with mongoRepository insert.

Below is the code for BulkOperations and MongoRepository:

  @PostMapping("/using-repository")
    public ResponseEntity<Documents> createDocument(@RequestBody Documents documentsRequest) {
        Documents _document = documentsRepository.insert(new Documents(documentsRequest.getTitle(), documentsRequest.getDescription()));
        return new ResponseEntity<>(_document, HttpStatus.CREATED);
    }

    @PostMapping("/using-template")
    public ResponseEntity<List<BulkWriteInsert>> createUsingTemplate(@RequestBody Documents documentsRequest) {
        Documents _document = new Documents(documentsRequest.getTitle(), documentsRequest.getDescription());
        List<Documents> validDocuments = new ArrayList<>();
        validDocuments.add(_document);
        BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Documents.class);
        bulkOperations.insert(validDocuments);
        return new ResponseEntity<>(bulkOperations.execute().getInserts(),HttpStatus.CREATED);
    }

Full file:
https://github.com/sriramkishoren/spring-boot-mongo-version-issue/blob/main/src/main/java/org/example/controller/DocumentsController.java

Below is the entity class

public class Documents {
    @Id
    private String id;
    private String title;
    private String description;

    @CreatedDate
    private Date createdAt;

    @LastModifiedDate
    private Date lastUpdatedAt;

    @Version
    private Long version;

    public Documents() {
    }

    public Documents(String title, String description) {
        this.title = title;
        this.description = description;
    }

Full file:
https://github.com/sriramkishoren/spring-boot-mongo-version-issue/blob/main/src/main/java/org/example/entity/Documents.java

Issue Screenshot Link:
https://github.com/sriramkishoren/spring-boot-mongo-version-issue/blob/main/Screen%20Shot%202022-06-12%20at%2010.08.41%20AM.png

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 12, 2022
@mp911de
Copy link
Member

mp911de commented Jun 13, 2022

Bulk operations use a low-level bulk mechanism to run a batch of insert/update/upsert/delete operations. Optimistic locking doesn't work well in such a scenario because the actual update happens at a later time while versioning details need to be maintained meanwhile.

We do not have a mechanism to return immutable updated objects because the API for insert returns BulkOperations. You can end up with a state where the object is prepared for an insert with the version counter incremented, but you never executed the bulk and so your in-memory state is inconsistent.

Updating objects isn't supported either through the Bulk API as Bulk updates require Update and Query objects instead of entities.

We should document these limitations.

@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Jun 13, 2022
@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged for: team-attention An issue we need to discuss as a team to make progress labels Aug 22, 2022
@mp911de mp911de self-assigned this Aug 22, 2022
@mp911de mp911de changed the title Annotation @version is not working in mongodb BulkOperations insert operation Document BulkOperations limitations Aug 23, 2022
mp911de added a commit that referenced this issue Aug 23, 2022
@mp911de mp911de added this to the 3.4.3 (2021.2.3) milestone Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants