Search in sources :

Example 16 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project spring-data-mongodb by spring-projects.

the class SimpleMongoRepository method delete.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
	 */
@Override
public void delete(T entity) {
    Assert.notNull(entity, "The given entity must not be null!");
    DeleteResult deleteResult = mongoOperations.remove(entity, entityInformation.getCollectionName());
    if (entityInformation.isVersioned() && deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 0) {
        throw new OptimisticLockingFailureException(String.format("The entity with id %s with version %s in %s cannot be deleted! Was it modified or deleted in the meantime?", entityInformation.getId(entity), entityInformation.getVersion(entity), entityInformation.getCollectionName()));
    }
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 17 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project spring-data-mongodb by spring-projects.

the class ExecutableRemoveOperationSupportTests method removeAllMatchingWithAlternateDomainTypeAndCollection.

// DATAMONGO-1563
@Test
void removeAllMatchingWithAlternateDomainTypeAndCollection() {
    DeleteResult result = template.remove(Jedi.class).inCollection(STAR_WARS).matching(query(where("name").is("luke"))).all();
    assertThat(result.getDeletedCount()).isEqualTo(1L);
}
Also used : DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.jupiter.api.Test)

Example 18 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project spring-data-mongodb by spring-projects.

the class ExecutableRemoveOperationSupportTests method removeAllMatchingCriteria.

// DATAMONGO-2416
@Test
void removeAllMatchingCriteria() {
    DeleteResult result = template.remove(Person.class).matching(where("firstname").is("han")).all();
    assertThat(result.getDeletedCount()).isEqualTo(1L);
}
Also used : DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.jupiter.api.Test)

Example 19 with DeleteResult

use of com.mongodb.client.result.DeleteResult in project spring-data-mongodb by spring-projects.

the class ExecutableRemoveOperationSupportTests method removeAll.

// DATAMONGO-1563
@Test
void removeAll() {
    DeleteResult result = template.remove(Person.class).all();
    assertThat(result.getDeletedCount()).isEqualTo(2L);
}
Also used : DeleteResult(com.mongodb.client.result.DeleteResult) Test(org.junit.jupiter.api.Test)

Example 20 with DeleteResult

use of com.mongodb.client.result.DeleteResult 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) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Version(org.springframework.data.annotation.Version) CloseableIterator(org.springframework.data.util.CloseableIterator) Index(org.springframework.data.mongodb.core.index.Index) BigDecimal(java.math.BigDecimal) AbstractMongoEventListener(org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DBObject(com.mongodb.DBObject) UpdateResult(com.mongodb.client.result.UpdateResult) Duration(java.time.Duration) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Assertions(org.assertj.core.api.Assertions) Update(org.springframework.data.mongodb.core.query.Update) BigInteger(java.math.BigInteger) Sort(org.springframework.data.domain.Sort) ClassUtils(org.springframework.util.ClassUtils) With(lombok.With) MongoException(com.mongodb.MongoException) PageRequest(org.springframework.data.domain.PageRequest) EqualsAndHashCode(lombok.EqualsAndHashCode) Instant(java.time.Instant) IndexField(org.springframework.data.mongodb.core.index.IndexField) Collectors(java.util.stream.Collectors) Client(org.springframework.data.mongodb.test.util.Client) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.jupiter.api.Test) InvalidMongoDbApiUsageException(org.springframework.data.mongodb.InvalidMongoDbApiUsageException) 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) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) MongoId(org.springframework.data.mongodb.core.mapping.MongoId) AtomicReference(java.util.concurrent.atomic.AtomicReference) Value(lombok.Value) Filters(com.mongodb.client.model.Filters) MappingException(org.springframework.data.mapping.MappingException) MongoCursor(com.mongodb.client.MongoCursor) Direction(org.springframework.data.domain.Sort.Direction) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) 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) BasicDBObject(com.mongodb.BasicDBObject) IndexOptions(com.mongodb.client.model.IndexOptions) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) LastModifiedDate(org.springframework.data.annotation.LastModifiedDate) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Field(org.springframework.data.mongodb.core.mapping.Field) MongoVersion(org.springframework.data.mongodb.test.util.MongoVersion) AfterEach(org.junit.jupiter.api.AfterEach) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) AfterSaveEvent(org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent) ChronoUnit(java.time.temporal.ChronoUnit) MongoTestTemplate(org.springframework.data.mongodb.test.util.MongoTestTemplate) DBRef(com.mongodb.DBRef) Data(lombok.Data) IsNewAwareAuditingHandler(org.springframework.data.auditing.IsNewAwareAuditingHandler) ObjectId(org.bson.types.ObjectId) DeleteResult(com.mongodb.client.result.DeleteResult) AllArgsConstructor(lombok.AllArgsConstructor) WriteConcern(com.mongodb.WriteConcern) MongoClientExtension(org.springframework.data.mongodb.test.util.MongoClientExtension) 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.jupiter.api.Test)

Aggregations

DeleteResult (com.mongodb.client.result.DeleteResult)46 Document (org.bson.Document)20 UpdateResult (com.mongodb.client.result.UpdateResult)10 BsonDocument (org.bson.BsonDocument)9 Test (org.junit.jupiter.api.Test)9 BasicDBObject (com.mongodb.BasicDBObject)7 WriteConcern (com.mongodb.WriteConcern)6 Bson (org.bson.conversions.Bson)6 MongoException (com.mongodb.MongoException)5 ArrayList (java.util.ArrayList)5 DataAccessException (org.springframework.dao.DataAccessException)5 MongoGridFSException (com.mongodb.MongoGridFSException)4 MongoClient (com.mongodb.client.MongoClient)4 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)4 ReadPreference (com.mongodb.ReadPreference)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 InsertOneResult (com.mongodb.client.result.InsertOneResult)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)3 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)3 ObjectId (org.bson.types.ObjectId)3