diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IgnoreIndexes.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IgnoreIndexes.java new file mode 100644 index 0000000000..112d87d316 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/IgnoreIndexes.java @@ -0,0 +1,35 @@ +/* + * Copyright 2021-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core.index; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Ignore idexes defined in embedded object class + * + * @author Rocco Lagrotteria + * @since 4.3 + */ +@Documented +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = { ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD }) +public @interface IgnoreIndexes { + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java index 7c4325c453..0a936e72a9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java @@ -161,7 +161,7 @@ private void potentiallyAddIndexForProperty(MongoPersistentEntity root, Mongo return; } - if (persistentProperty.isEntity()) { + if (persistentProperty.isEntity() && !persistentProperty.isAnnotationPresent(IgnoreIndexes.class) ) { indexes.addAll(resolveIndexForEntity(mappingContext.getPersistentEntity(persistentProperty), persistentProperty.isUnwrapped() ? "" : persistentProperty.getFieldName(), Path.of(persistentProperty), root.getCollection(), guard)); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 5660689695..cb7f881556 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -16,8 +16,9 @@ package org.springframework.data.mongodb.core.index; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; -import static org.springframework.data.mongodb.test.util.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -288,6 +289,22 @@ public void resolvesPartialFilter() { org.bson.Document.parse("{'value': {'$exists': true}}")); } + @Test // DATAMONGO-1965 + public void indexOnLevelOneIsIgnoredCorrectly() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType(IgnoreIndexesOnLevelOne.class); + + assertThat(indexDefinitions).hasSize(0); + } + + @Test // DATAMONGO-1965 + public void deeplyNestedIndexIsIgnoredCorrectly() { + + List indexDefinitions = prepareMappingContextAndResolveIndexForType(IgnoreIndexexOnLevelTwo.class); + + assertThat(indexDefinitions).hasSize(0); + } + @Document("Zero") class IndexOnLevelZero { @Indexed String indexedProperty; @@ -303,6 +320,18 @@ class IndexOnLevelTwo { IndexOnLevelOne one; } + @Document("IgnoreOne") + class IgnoreIndexesOnLevelOne { + @IgnoreIndexes + IndexOnLevelZero zero; + } + + @Document("IgnoreOne") + class IgnoreIndexexOnLevelTwo { + @IgnoreIndexes + IndexOnLevelOne one; + } + @Document("WithOptionsOnIndexedProperty") class WithOptionsOnIndexedProperty {