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
This commit is contained in:
Mark Paluch
2024-06-11 14:46:49 +02:00
parent 1e32e3b0e7
commit 17d8a425e8
5 changed files with 18 additions and 10 deletions

View File

@@ -1603,9 +1603,7 @@ public class MongoTemplate
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
Document replacement = updateContext.getMappedUpdate(entity);
Document filter = updateContext.getMappedQuery(entity);
Document filter = updateContext.getReplacementQuery();
if (updateContext.requiresShardKey(filter, entity)) {
if (entity.getShardKey().isImmutable()) {

View File

@@ -400,7 +400,7 @@ class QueryOperations {
for (Entry<String, Object> entry : fields.entrySet()) {
if (entry.getValue()instanceof MongoExpression mongoExpression) {
if (entry.getValue() instanceof MongoExpression mongoExpression) {
AggregationOperationContext ctx = entity == null ? Aggregation.DEFAULT_CONTEXT
: new RelaxedTypeBasedAggregationOperationContext(entity.getType(), mappingContext, queryMapper);
@@ -809,13 +809,23 @@ class QueryOperations {
@Override
<T> Document getMappedQuery(@Nullable MongoPersistentEntity<T> domainType) {
return applyIsolation(super.getMappedQuery(domainType));
}
Document mappedQuery = super.getMappedQuery(domainType);
/**
* A replacement query that is derived from the already {@link MappedDocument}.
*
* @return
*/
Document getReplacementQuery() {
return applyIsolation(getQueryObject());
}
private Document applyIsolation(Document mappedQuery) {
if (multi && update != null && update.isIsolated() && !mappedQuery.containsKey("$isolated")) {
mappedQuery = new Document(mappedQuery);
mappedQuery.put("$isolated", 1);
}
return mappedQuery;
}

View File

@@ -1649,7 +1649,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
Document filter = updateContext.getMappedQuery(entity);
Document filter = updateContext.getReplacementQuery();
Document replacement = updateContext.getMappedUpdate(entity);
Mono<Document> deferredFilter;

View File

@@ -728,7 +728,7 @@ public class QueryMapper {
} else if (isKeyword(key)) {
resultDbo.put(key, convertIdField(documentField, entry.getValue()));
} else {
if(documentField.getProperty() != null && documentField.getProperty().isEntity()) {
if (documentField.getProperty() != null && documentField.getProperty().isEntity()) {
Field propertyField = createPropertyField(documentField.getPropertyEntity(), key, mappingContext);
resultDbo.put(key, getMappedValue(propertyField, entry.getValue()));
} else {

View File

@@ -1416,7 +1416,7 @@ public class MongoTemplateTests {
id.id = Instant.now().minusSeconds(2);
id.first = "foo";
id.second = "bar";
id.time = Instant.now().minusSeconds(3);
id.id = Instant.now().minusSeconds(3);
TypeWithMyId source = new TypeWithMyId();
source.id = id;
@@ -4423,7 +4423,7 @@ public class MongoTemplateTests {
String first;
String second;
Instant time;
Instant id;
@Field("t") Instant time;
}