use of com.mongodb.reactivestreams.client.MongoCollection in project mongo-java-driver by mongodb.
the class BatchCursorFluxTest method ensureExists.
/**
* This method ensures that the server considers the specified {@code collection} existing for the purposes of, e.g.,
* {@link MongoCollection#drop()}, instead of replying with
* {@code "errmsg": "ns not found", "code": 26, "codeName": "NamespaceNotFound"}.
*
* @return {@code operationTime} starting at which the {@code collection} is guaranteed to exist.
*/
private static BsonTimestamp ensureExists(final MongoClient client, final MongoCollection<Document> collection) {
BsonValue insertedId = Mono.from(collection.insertOne(Document.parse("{}"))).map(InsertOneResult::getInsertedId).block(TIMEOUT_DURATION);
BsonArray deleteStatements = new BsonArray();
deleteStatements.add(new BsonDocument().append("q", new BsonDocument().append("_id", insertedId)).append("limit", new BsonInt32(1)));
Publisher<Document> deletePublisher = client.getDatabase(collection.getNamespace().getDatabaseName()).runCommand(new BsonDocument().append("delete", new BsonString(collection.getNamespace().getCollectionName())).append("deletes", deleteStatements));
BsonTimestamp operationTime = Mono.from(deletePublisher).map(doc -> doc.get("operationTime", BsonTimestamp.class)).block(TIMEOUT_DURATION);
assertNotNull(operationTime);
return operationTime;
}
use of com.mongodb.reactivestreams.client.MongoCollection in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method doRemove.
protected <T> Mono<DeleteResult> doRemove(String collectionName, Query query, @Nullable Class<T> entityClass) {
if (query == null) {
throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null!");
}
Assert.hasText(collectionName, "Collection name must not be null or empty!");
MongoPersistentEntity<?> entity = getPersistentEntity(entityClass);
DeleteContext deleteContext = queryOperations.deleteQueryContext(query);
Document queryObject = deleteContext.getMappedQuery(entity);
DeleteOptions deleteOptions = deleteContext.getDeleteOptions(entityClass);
Document removeQuery = deleteContext.getMappedQuery(entity);
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, removeQuery);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
return execute(collectionName, collection -> {
maybeEmitEvent(new BeforeDeleteEvent<>(removeQuery, entityClass, collectionName));
MongoCollection<Document> collectionToUse = prepareCollection(collection, writeConcernToUse);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Remove using query: %s in collection: %s.", serializeToJsonSafely(removeQuery), collectionName));
}
if (query.getLimit() > 0 || query.getSkip() > 0) {
FindPublisher<Document> cursor = new QueryFindPublisherPreparer(query, entityClass).prepare(//
collection.find(removeQuery)).projection(MappedDocument.getIdOnlyProjection());
return //
Flux.from(cursor).map(//
MappedDocument::of).map(//
MappedDocument::getId).collectList().flatMapMany(val -> {
return collectionToUse.deleteMany(MappedDocument.getIdIn(val), deleteOptions);
});
} else {
return collectionToUse.deleteMany(removeQuery, deleteOptions);
}
}).doOnNext(//
it -> maybeEmitEvent(new AfterDeleteEvent<>(queryObject, entityClass, collectionName))).next();
}
use of com.mongodb.reactivestreams.client.MongoCollection in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method saveDocument.
protected Mono<Object> saveDocument(String collectionName, Document document, Class<?> entityClass) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Saving Document containing fields: %s", document.keySet()));
}
return createMono(collectionName, collection -> {
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, document, null);
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
MappedDocument mapped = MappedDocument.of(document);
MongoCollection<Document> collectionToUse = //
writeConcernToUse == null ? //
collection : collection.withWriteConcern(writeConcernToUse);
Publisher<?> publisher;
if (!mapped.hasId()) {
publisher = collectionToUse.insertOne(document);
} else {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
UpdateContext updateContext = queryOperations.replaceSingleContext(mapped, true);
Document filter = updateContext.getMappedQuery(entity);
Document replacement = updateContext.getMappedUpdate(entity);
Mono<Document> deferredFilter;
if (updateContext.requiresShardKey(filter, entity)) {
if (entity.getShardKey().isImmutable()) {
deferredFilter = Mono.just(updateContext.applyShardKey(entity, filter, null));
} else {
deferredFilter = Mono.from(collection.find(filter, Document.class).projection(updateContext.getMappedShardKey(entity)).first()).defaultIfEmpty(replacement).map(it -> updateContext.applyShardKey(entity, filter, it));
}
} else {
deferredFilter = Mono.just(filter);
}
publisher = deferredFilter.flatMapMany(it -> collectionToUse.replaceOne(it, replacement, updateContext.getReplaceOptions(entityClass)));
}
return Mono.from(publisher).map(o -> mapped.getId());
});
}
Aggregations