Skip to content

Commit 17d8a42

Browse files
committed
Polishing.
Avoid duplicate query mapping for document replacement operations when the filter query can be determined from the already mapped _id field. See #4707 Original pull request: #4719
1 parent 1e32e3b commit 17d8a42

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1603,9 +1603,7 @@ protected Object saveDocument(String collectionName, Document dbDoc, Class<?> en
16031603
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
16041604
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
16051605
Document replacement = updateContext.getMappedUpdate(entity);
1606-
1607-
Document filter = updateContext.getMappedQuery(entity);
1608-
1606+
Document filter = updateContext.getReplacementQuery();
16091607
if (updateContext.requiresShardKey(filter, entity)) {
16101608

16111609
if (entity.getShardKey().isImmutable()) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ private Document evaluateFields(@Nullable MongoPersistentEntity<?> entity) {
400400

401401
for (Entry<String, Object> entry : fields.entrySet()) {
402402

403-
if (entry.getValue()instanceof MongoExpression mongoExpression) {
403+
if (entry.getValue() instanceof MongoExpression mongoExpression) {
404404

405405
AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
406406
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
@@ -809,13 +809,23 @@ ReplaceOptions getReplaceOptions(@Nullable Class<?> domainType, @Nullable Consum
809809

810810
@Override
811811
<T> Document getMappedQuery(@Nullable MongoPersistentEntity<T> domainType) {
812+
return applyIsolation(super.getMappedQuery(domainType));
813+
}
812814

813-
Document mappedQuery = super.getMappedQuery(domainType);
815+
/**
816+
* A replacement query that is derived from the already {@link MappedDocument}.
817+
*
818+
* @return
819+
*/
820+
Document getReplacementQuery() {
821+
return applyIsolation(getQueryObject());
822+
}
814823

824+
private Document applyIsolation(Document mappedQuery) {
815825
if (multi && update != null && update.isIsolated() && !mappedQuery.containsKey("$isolated")) {
826+
mappedQuery = new Document(mappedQuery);
816827
mappedQuery.put("$isolated", 1);
817828
}
818-
819829
return mappedQuery;
820830
}
821831

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ protected Mono<Object> saveDocument(String collectionName, Document document, Cl
16491649

16501650
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
16511651
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
1652-
Document filter = updateContext.getMappedQuery(entity);
1652+
Document filter = updateContext.getReplacementQuery();
16531653
Document replacement = updateContext.getMappedUpdate(entity);
16541654

16551655
Mono<Document> deferredFilter;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ private Object convertIdField(Field documentField, Object source) {
728728
} else if (isKeyword(key)) {
729729
resultDbo.put(key, convertIdField(documentField, entry.getValue()));
730730
} else {
731-
if(documentField.getProperty() != null && documentField.getProperty().isEntity()) {
731+
if (documentField.getProperty() != null && documentField.getProperty().isEntity()) {
732732
Field propertyField = createPropertyField(documentField.getPropertyEntity(), key, mappingContext);
733733
resultDbo.put(key, getMappedValue(propertyField, entry.getValue()));
734734
} else {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ public void storesAndRemovesTypeWithComplexId() {
14161416
id.id = Instant.now().minusSeconds(2);
14171417
id.first = "foo";
14181418
id.second = "bar";
1419-
id.time = Instant.now().minusSeconds(3);
1419+
id.id = Instant.now().minusSeconds(3);
14201420

14211421
TypeWithMyId source = new TypeWithMyId();
14221422
source.id = id;
@@ -4423,7 +4423,7 @@ static class MyId {
44234423

44244424
String first;
44254425
String second;
4426-
Instant time;
4426+
Instant id;
44274427

44284428
@Field("t") Instant time;
44294429
}

0 commit comments

Comments
 (0)