Skip to content

Commit 13cf53a

Browse files
committed
Example with adding indexes from code and like annotation.
1 parent 4e02612 commit 13cf53a

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎spring-data-mongodb/src/main/java/org/baeldung/model/User.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.baeldung.model;
22

33
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.index.CompoundIndex;
5+
import org.springframework.data.mongodb.core.index.CompoundIndexes;
6+
import org.springframework.data.mongodb.core.index.Indexed;
7+
import org.springframework.data.mongodb.core.mapping.DBRef;
48
import org.springframework.data.mongodb.core.mapping.Document;
9+
import org.springframework.data.mongodb.core.mapping.Field;
510

611
import com.mysema.query.annotations.QueryEntity;
712

@@ -13,6 +18,10 @@ public class User {
1318
private String id;
1419
private String name;
1520
private Integer age;
21+
@DBRef
22+
@Indexed
23+
@Field("email")
24+
private EmailAddress emailAddress;
1625

1726
public String getId() {
1827
return id;
@@ -37,4 +46,12 @@ public Integer getAge() {
3746
public void setAge(final Integer age) {
3847
this.age = age;
3948
}
49+
50+
public EmailAddress getEmailAddress() {
51+
return emailAddress;
52+
}
53+
54+
public void setEmailAddress(EmailAddress emailAddress) {
55+
this.emailAddress = emailAddress;
56+
}
4057
}

‎spring-data-mongodb/src/test/java/org/baeldung/repository/UserRepositoryIntegrationTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77

88
import org.baeldung.config.MongoConfig;
9+
import org.baeldung.model.EmailAddress;
910
import org.baeldung.model.User;
1011
import org.junit.After;
1112
import org.junit.Before;
@@ -16,7 +17,10 @@
1617
import org.springframework.data.domain.PageRequest;
1718
import org.springframework.data.domain.Pageable;
1819
import org.springframework.data.domain.Sort;
20+
import org.springframework.data.domain.Sort.Direction;
1921
import org.springframework.data.mongodb.core.MongoOperations;
22+
import org.springframework.data.mongodb.core.index.Index;
23+
import org.springframework.data.mongodb.core.index.IndexInfo;
2024
import org.springframework.data.mongodb.core.query.Criteria;
2125
import org.springframework.data.mongodb.core.query.Query;
2226
import org.springframework.test.context.ContextConfiguration;
@@ -145,4 +149,30 @@ public void givenUsersExist_whenFindingAllUsersWithPagination_thenUsersAreFoundA
145149
assertThat(users.size(), is(1));
146150
assertThat(page.getTotalPages(), is(2));
147151
}
152+
153+
@Test
154+
public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndexIsExisted() {
155+
User user = new User();
156+
user.setName("Brendan");
157+
EmailAddress emailAddress = new EmailAddress("a@gmail.com");
158+
mongoOps.insert(emailAddress);
159+
user.setEmailAddress(emailAddress);
160+
mongoOps.insert(user);
161+
162+
List<IndexInfo> indexInfos = mongoOps.indexOps("user").getIndexInfo();
163+
164+
assertThat(indexInfos.size(), is(1));
165+
}
166+
167+
@Test
168+
public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsExisted() {
169+
User user = new User();
170+
user.setName("Brendan");
171+
mongoOps.indexOps(User.class).ensureIndex(new Index().on("name", Direction.ASC));
172+
mongoOps.insert(user);
173+
174+
List<IndexInfo> indexInfos = mongoOps.indexOps("user").getIndexInfo();
175+
176+
assertThat(indexInfos.size(), is(2));
177+
}
148178
}

0 commit comments

Comments
 (0)