use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.
the class MongoTemplateTests method testEnsureIndex.
@Test
@SuppressWarnings("deprecation")
public void testEnsureIndex() throws Exception {
Person p1 = new Person("Oliver");
p1.setAge(25);
template.insert(p1);
Person p2 = new Person("Sven");
p2.setAge(40);
template.insert(p2);
template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
List<org.bson.Document> indexInfo = new ArrayList<org.bson.Document>();
coll.listIndexes().into(indexInfo);
assertThat(indexInfo.size(), is(2));
Object indexKey = null;
boolean unique = false;
for (org.bson.Document ix : indexInfo) {
if ("age_-1".equals(ix.get("name"))) {
indexKey = ix.get("key");
unique = (Boolean) ix.get("unique");
assertThat(ix.get("dropDups"), is(nullValue()));
}
}
assertThat(((org.bson.Document) indexKey), IsMapContaining.<String, Object>hasEntry("age", -1));
assertThat(unique, is(true));
List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
assertThat(indexInfoList.size(), is(2));
IndexInfo ii = indexInfoList.get(1);
assertThat(ii.isUnique(), is(true));
assertThat(ii.isSparse(), is(false));
List<IndexField> indexFields = ii.getIndexFields();
IndexField field = indexFields.get(0);
assertThat(field, is(IndexField.create("age", Direction.DESC)));
}
use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.
the class IndexUnitTests method testWithAscendingIndex.
@Test
public void testWithAscendingIndex() {
Index i = new Index().on("name", Direction.ASC);
assertEquals(Document.parse("{ \"name\" : 1}"), i.getIndexKeys());
}
use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.
the class IndexUnitTests method ensuresPropertyOrder.
@Test
public void ensuresPropertyOrder() {
Index on = new Index("foo", Direction.ASC).on("bar", Direction.ASC);
assertThat(on.getIndexKeys(), is(Document.parse("{ \"foo\" : 1 , \"bar\" : 1}")));
}
use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.
the class IndexUnitTests method testNamedMultiFieldUniqueIndex.
@Test
public void testNamedMultiFieldUniqueIndex() {
Index i = new Index().on("name", Direction.ASC).on("age", Direction.DESC);
i.named("test").unique();
assertEquals(Document.parse("{ \"name\" : 1 , \"age\" : -1}"), i.getIndexKeys());
assertEquals(Document.parse("{ \"name\" : \"test\" , \"unique\" : true}"), i.getIndexOptions());
}
use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplateTests method throwsExceptionForIndexViolationIfConfigured.
// DATAMONGO-1444
@Test
public void throwsExceptionForIndexViolationIfConfigured() {
ReactiveMongoTemplate template = new ReactiveMongoTemplate(factory);
template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
StepVerifier.create(//
template.indexOps(Person.class).ensureIndex(//
new Index().on("firstName", Direction.DESC).unique())).expectNextCount(//
1).verifyComplete();
Person person = new Person(new ObjectId(), "Amol");
person.setAge(28);
StepVerifier.create(template.save(person)).expectNextCount(1).verifyComplete();
person = new Person(new ObjectId(), "Amol");
person.setAge(28);
StepVerifier.create(template.save(person)).expectError(DataIntegrityViolationException.class).verify();
}
Aggregations