Search in sources :

Example 6 with Index

use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsTests method shouldApplyPartialFilterCorrectly.

// DATAMONGO-1682
@Test
public void shouldApplyPartialFilterCorrectly() {
    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
    IndexDefinition id = new Index().named("partial-with-criteria").on("k3y", Direction.ASC).partial(of(where("q-t-y").gte(10)));
    StepVerifier.create(indexOps.ensureIndex(id)).expectNextCount(1).verifyComplete();
    // 
    StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("partial-with-criteria"))).consumeNextWith(indexInfo -> {
        assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"q-t-y\" : { \"$gte\" : 10 } }");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Collation(org.springframework.data.mongodb.core.query.Collation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) MongoClient(com.mongodb.reactivestreams.client.MongoClient) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Before(org.junit.Before) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Configuration(org.springframework.context.annotation.Configuration) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) Test(org.junit.Test)

Example 7 with Index

use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsTests method shouldApplyPartialFilterWithMappedPropertyCorrectly.

// DATAMONGO-1682
@Test
public void shouldApplyPartialFilterWithMappedPropertyCorrectly() {
    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
    IndexDefinition id = new Index().named("partial-with-mapped-criteria").on("k3y", Direction.ASC).partial(of(where("quantity").gte(10)));
    StepVerifier.create(indexOps.ensureIndex(id)).expectNextCount(1).verifyComplete();
    // 
    StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("partial-with-mapped-criteria"))).consumeNextWith(indexInfo -> {
        assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"qty\" : { \"$gte\" : 10 } }");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Collation(org.springframework.data.mongodb.core.query.Collation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) MongoClient(com.mongodb.reactivestreams.client.MongoClient) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Before(org.junit.Before) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Configuration(org.springframework.context.annotation.Configuration) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) Test(org.junit.Test)

Example 8 with Index

use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.

the class DefaultReactiveIndexOperationsTests method shouldFavorExplicitMappingHintViaClass.

// DATAMONGO-1682
@Test
public void shouldFavorExplicitMappingHintViaClass() {
    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
    IndexDefinition id = new Index().named("partial-with-inheritance").on("k3y", Direction.ASC).partial(of(where("age").gte(10)));
    indexOps = new DefaultReactiveIndexOperations(template, this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class), new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    StepVerifier.create(indexOps.ensureIndex(id)).expectNextCount(1).verifyComplete();
    // 
    StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("partial-with-inheritance"))).consumeNextWith(indexInfo -> {
        assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"a_g_e\" : { \"$gte\" : 10 } }");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Collation(org.springframework.data.mongodb.core.query.Collation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) MongoClient(com.mongodb.reactivestreams.client.MongoClient) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Before(org.junit.Before) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Configuration(org.springframework.context.annotation.Configuration) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Test(org.junit.Test)

Example 9 with Index

use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method throwsExceptionForIndexViolationIfConfigured.

// DATAMONGO-480
@Test
public void throwsExceptionForIndexViolationIfConfigured() {
    MongoTemplate template = new MongoTemplate(factory);
    template.setWriteResultChecking(WriteResultChecking.EXCEPTION);
    template.indexOps(Person.class).ensureIndex(new Index().on("firstName", Direction.DESC).unique());
    Person person = new Person(new ObjectId(), "Amol");
    person.setAge(28);
    template.save(person);
    person = new Person(new ObjectId(), "Amol");
    person.setAge(28);
    try {
        template.save(person);
        fail("Expected DataIntegrityViolationException!");
    } catch (DataIntegrityViolationException e) {
        assertThat(e.getMessage(), containsString("E11000 duplicate key error"));
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Index(org.springframework.data.mongodb.core.index.Index) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 10 with Index

use of org.springframework.data.mongodb.core.index.Index in project spring-data-mongodb by spring-projects.

the class IndexUnitTests method testWithSparse.

@Test
public void testWithSparse() {
    Index i = new Index().on("name", Direction.ASC);
    i.sparse().unique();
    assertEquals(Document.parse("{ \"name\" : 1}"), i.getIndexKeys());
    assertEquals(Document.parse("{ \"unique\" : true , \"sparse\" : 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)

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