Search in sources :

Example 1 with Field

use of org.springframework.data.mongodb.core.mapping.Field in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method removeShouldConsiderLimit.

// DATAMONGO-1870
@Test
public void removeShouldConsiderLimit() {
    List<Sample> samples = // 
    IntStream.range(0, 100).mapToObj(// 
    i -> new Sample("id-" + i, i % 2 == 0 ? "stark" : "lannister")).collect(Collectors.toList());
    template.insertAll(samples);
    DeleteResult wr = template.remove(query(where("field").is("lannister")).limit(25), Sample.class);
    assertThat(wr.getDeletedCount()).isEqualTo(25L);
    assertThat(template.count(new Query(), Sample.class)).isEqualTo(75L);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) CustomConversions(org.springframework.data.convert.CustomConversions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Matchers.not(org.hamcrest.Matchers.not) Version(org.springframework.data.annotation.Version) CloseableIterator(org.springframework.data.util.CloseableIterator) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClientVersion(org.springframework.data.mongodb.util.MongoClientVersion) Index(org.springframework.data.mongodb.core.index.Index) Assert.assertThat(org.junit.Assert.assertThat) BigDecimal(java.math.BigDecimal) AbstractMongoEventListener(org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) UpdateResult(com.mongodb.client.result.UpdateResult) After(org.junit.After) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Update(org.springframework.data.mongodb.core.query.Update) BigInteger(java.math.BigInteger) Sort(org.springframework.data.domain.Sort) ClassUtils(org.springframework.util.ClassUtils) MongoException(com.mongodb.MongoException) Mongo(com.mongodb.Mongo) PageRequest(org.springframework.data.domain.PageRequest) EqualsAndHashCode(lombok.EqualsAndHashCode) IndexField(org.springframework.data.mongodb.core.index.IndexField) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Assertions.fail(org.assertj.core.api.Assertions.fail) InvalidMongoDbApiUsageException(org.springframework.data.mongodb.InvalidMongoDbApiUsageException) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) FindIterable(com.mongodb.client.FindIterable) Id(org.springframework.data.annotation.Id) ListIndexesIterable(com.mongodb.client.ListIndexesIterable) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) IntStream(java.util.stream.IntStream) ReadPreference(com.mongodb.ReadPreference) java.util(java.util) DataAccessException(org.springframework.dao.DataAccessException) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoCollection(com.mongodb.client.MongoCollection) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) RunWith(org.junit.runner.RunWith) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) MappingException(org.springframework.data.mapping.MappingException) MongoCursor(com.mongodb.client.MongoCursor) MongoVersionRule(org.springframework.data.mongodb.test.util.MongoVersionRule) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) ExpectedException(org.junit.rules.ExpectedException) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) Before(org.junit.Before) BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) Converter(org.springframework.core.convert.converter.Converter) LazyLoadingProxy(org.springframework.data.mongodb.core.convert.LazyLoadingProxy) ObjectUtils(org.springframework.util.ObjectUtils) DateTime(org.joda.time.DateTime) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Field(org.springframework.data.mongodb.core.mapping.Field) IsMapContaining(org.hamcrest.collection.IsMapContaining) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Rule(org.junit.Rule) DBRef(com.mongodb.DBRef) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Data(lombok.Data) ObjectId(org.bson.types.ObjectId) DeleteResult(com.mongodb.client.result.DeleteResult) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) Assert(org.junit.Assert) WriteConcern(com.mongodb.WriteConcern) NoArgsConstructor(lombok.NoArgsConstructor) StringUtils(org.springframework.util.StringUtils) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.Test)

Example 2 with Field

use of org.springframework.data.mongodb.core.mapping.Field in project spring-data-mongodb by spring-projects.

the class MongoTemplateTests method removeShouldConsiderSkipAndSort.

// DATAMONGO-1870
@Test
public void removeShouldConsiderSkipAndSort() {
    List<Sample> samples = // 
    IntStream.range(0, 100).mapToObj(// 
    i -> new Sample("id-" + i, i % 2 == 0 ? "stark" : "lannister")).collect(Collectors.toList());
    template.insertAll(samples);
    DeleteResult wr = template.remove(new Query().skip(25).with(Sort.by("field")), Sample.class);
    assertThat(wr.getDeletedCount()).isEqualTo(75L);
    assertThat(template.count(new Query(), Sample.class)).isEqualTo(25L);
    assertThat(template.count(query(where("field").is("lannister")), Sample.class)).isEqualTo(25L);
    assertThat(template.count(query(where("field").is("stark")), Sample.class)).isEqualTo(0L);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) CustomConversions(org.springframework.data.convert.CustomConversions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Matchers.not(org.hamcrest.Matchers.not) Version(org.springframework.data.annotation.Version) CloseableIterator(org.springframework.data.util.CloseableIterator) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClientVersion(org.springframework.data.mongodb.util.MongoClientVersion) Index(org.springframework.data.mongodb.core.index.Index) Assert.assertThat(org.junit.Assert.assertThat) BigDecimal(java.math.BigDecimal) AbstractMongoEventListener(org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) UpdateResult(com.mongodb.client.result.UpdateResult) After(org.junit.After) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Update(org.springframework.data.mongodb.core.query.Update) BigInteger(java.math.BigInteger) Sort(org.springframework.data.domain.Sort) ClassUtils(org.springframework.util.ClassUtils) MongoException(com.mongodb.MongoException) Mongo(com.mongodb.Mongo) PageRequest(org.springframework.data.domain.PageRequest) EqualsAndHashCode(lombok.EqualsAndHashCode) IndexField(org.springframework.data.mongodb.core.index.IndexField) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Assertions.fail(org.assertj.core.api.Assertions.fail) InvalidMongoDbApiUsageException(org.springframework.data.mongodb.InvalidMongoDbApiUsageException) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) FindIterable(com.mongodb.client.FindIterable) Id(org.springframework.data.annotation.Id) ListIndexesIterable(com.mongodb.client.ListIndexesIterable) GeoJsonPoint(org.springframework.data.mongodb.core.geo.GeoJsonPoint) IntStream(java.util.stream.IntStream) ReadPreference(com.mongodb.ReadPreference) java.util(java.util) DataAccessException(org.springframework.dao.DataAccessException) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoCollection(com.mongodb.client.MongoCollection) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) RunWith(org.junit.runner.RunWith) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) MappingException(org.springframework.data.mapping.MappingException) MongoCursor(com.mongodb.client.MongoCursor) MongoVersionRule(org.springframework.data.mongodb.test.util.MongoVersionRule) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) ExpectedException(org.junit.rules.ExpectedException) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) Before(org.junit.Before) BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) Converter(org.springframework.core.convert.converter.Converter) LazyLoadingProxy(org.springframework.data.mongodb.core.convert.LazyLoadingProxy) ObjectUtils(org.springframework.util.ObjectUtils) DateTime(org.joda.time.DateTime) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Field(org.springframework.data.mongodb.core.mapping.Field) IsMapContaining(org.hamcrest.collection.IsMapContaining) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Rule(org.junit.Rule) DBRef(com.mongodb.DBRef) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Data(lombok.Data) ObjectId(org.bson.types.ObjectId) DeleteResult(com.mongodb.client.result.DeleteResult) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) Assert(org.junit.Assert) WriteConcern(com.mongodb.WriteConcern) NoArgsConstructor(lombok.NoArgsConstructor) StringUtils(org.springframework.util.StringUtils) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.Test)

Aggregations

DBRef (com.mongodb.DBRef)2 Mongo (com.mongodb.Mongo)2 MongoException (com.mongodb.MongoException)2 ReadPreference (com.mongodb.ReadPreference)2 WriteConcern (com.mongodb.WriteConcern)2 FindIterable (com.mongodb.client.FindIterable)2 ListIndexesIterable (com.mongodb.client.ListIndexesIterable)2 MongoCollection (com.mongodb.client.MongoCollection)2 MongoCursor (com.mongodb.client.MongoCursor)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 UpdateResult (com.mongodb.client.result.UpdateResult)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Data (lombok.Data)2 EqualsAndHashCode (lombok.EqualsAndHashCode)2 NoArgsConstructor (lombok.NoArgsConstructor)2