Search in sources :

Example 21 with Index

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)));
}
Also used : Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Example 22 with Index

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());
}
Also used : GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Index(org.springframework.data.mongodb.core.index.Index) Test(org.junit.Test)

Example 23 with Index

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}")));
}
Also used : GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Index(org.springframework.data.mongodb.core.index.Index) Test(org.junit.Test)

Example 24 with Index

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());
}
Also used : GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Index(org.springframework.data.mongodb.core.index.Index) Test(org.junit.Test)

Example 25 with Index

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();
}
Also used : ObjectId(org.bson.types.ObjectId) Index(org.springframework.data.mongodb.core.index.Index) GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) VersionedPerson(org.springframework.data.mongodb.core.MongoTemplateTests.VersionedPerson) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Aggregations

Index (org.springframework.data.mongodb.core.index.Index)25 Test (org.junit.Test)18 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)11 IndexDefinition (org.springframework.data.mongodb.core.index.IndexDefinition)10 Document (org.bson.Document)7 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)7 Direction (org.springframework.data.domain.Sort.Direction)6 GeospatialIndex (org.springframework.data.mongodb.core.index.GeospatialIndex)6 MongoClient (com.mongodb.reactivestreams.client.MongoClient)5 MongoClients (com.mongodb.reactivestreams.client.MongoClients)5 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)5 Predicate (java.util.function.Predicate)5 Assertions (org.assertj.core.api.Assertions)5 Release (org.devgateway.ocds.persistence.mongo.Release)5 Assume (org.junit.Assume)5 Before (org.junit.Before)5 RunWith (org.junit.runner.RunWith)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Configuration (org.springframework.context.annotation.Configuration)5 AbstractReactiveMongoConfiguration (org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration)5