UpdateBatch

Updating fields of documents.

To make it easier to work with individual fields, the native MongoDB driver has the updateMany method, which can be used to filter and then update documents.

This is provided in en2do via the updateFieldsBy method operator or via the predefined updateAllFields(UpdateBatch updateBatch) method.

Example of using UpdateBatch:

// Remove the field "postalCode"
boolean success = repository.updateAllFields(UpdateBatch.of(FieldUpdate.remove("postalCode")));

// Rename the field "balance" to "balanceRenamed"
boolean success = repository.updateAllFields(UpdateBatch.of(FieldUpdate.rename("balance", "balanceRenamed"));

// Hard-Set the value of the field "postalCode" to 987654321
boolean success = repository.updateAllFields(UpdateBatch.of(FieldUpdate.set("postalCode", 987654321)));

To optimize performance in this respect, several FieldUpdate can be combined in one call.

Example of multiple FieldUpdate in one call:

boolean success = repository.updateAllFields(UpdateBatch.of(
                FieldUpdate.remove("postalCode"),
                FieldUpdate.rename("balance", "balanceRenamed"),
                FieldUpdate.set("postalCode", 987654321)
        ));

Last updated