下面列出了怎么用com.mongodb.client.result.InsertOneResult的API类实例代码及写法,或者点击链接到github查看源代码。
public CompletableFuture<AggregateEventAppendResult> insertOneByOneAsync(List<Document> documents) {
CompletableFuture<AggregateEventAppendResult> future = new CompletableFuture<>();
CountDownLatch latch = new CountDownLatch(documents.size());
for (Document document : documents) {
mongoClient.getDatabase(mongoConfiguration.getDatabaseName()).getCollection(mongoConfiguration.getEventCollectionName())
.insertOne(document).subscribe(new Subscriber<InsertOneResult>() {
@Override
public void onSubscribe(Subscription s) {
s.request(1);
}
@Override
public void onNext(InsertOneResult insertOneResult) {
latch.countDown();
}
@Override
public void onError(Throwable t) {
latch.countDown();
future.completeExceptionally(t);
}
@Override
public void onComplete() {
if (latch.getCount() == 0) {
AggregateEventAppendResult appendResult = new AggregateEventAppendResult();
appendResult.setEventAppendStatus(EventAppendStatus.Success);
future.complete(appendResult);
}
}
});
if (future.isDone()) {
break;
}
}
return future;
}
protected Uni<Void> insertDocs(ReactiveMongoClient mongoClient, String collection, int num) {
io.quarkus.mongodb.reactive.ReactiveMongoDatabase database = mongoClient.getDatabase(DATABASE);
io.quarkus.mongodb.reactive.ReactiveMongoCollection<Document> mongoCollection = database
.getCollection(collection);
List<CompletableFuture<InsertOneResult>> list = new ArrayList<>();
for (int i = 0; i < num; i++) {
Document doc = createDoc(i);
list.add(mongoCollection.insertOne(doc).subscribeAsCompletionStage());
}
CompletableFuture<InsertOneResult>[] array = list.toArray(new CompletableFuture[0]);
return Uni.createFrom().completionStage(CompletableFuture.allOf(array));
}
@Override
public void insertObject(final NoSqlObject<Document> object) {
try {
final Document unwrapped = object.unwrap();
LOGGER.debug("Inserting BSON Document {}", unwrapped);
InsertOneResult insertOneResult = this.collection.insertOne(unwrapped);
LOGGER.debug("Insert MongoDb result {}", insertOneResult);
} catch (final MongoException e) {
throw new AppenderLoggingException("Failed to write log event to MongoDB due to error: " + e.getMessage(),
e);
}
}
@Override
public Uni<InsertOneResult> insertOne(T t) {
return Wrappers.toUni(collection.insertOne(t));
}
@Override
public Uni<InsertOneResult> insertOne(T t, InsertOneOptions options) {
return Wrappers.toUni(collection.insertOne(t, options));
}
@Override
public Uni<InsertOneResult> insertOne(ClientSession clientSession, T t) {
return Wrappers.toUni(collection.insertOne(clientSession, t));
}
@Override
public Uni<InsertOneResult> insertOne(ClientSession clientSession, T t, InsertOneOptions options) {
return Wrappers.toUni(collection.insertOne(clientSession, t, options));
}
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param document the document to insert
* @return a {@link Uni} completed successfully when the operation completes, or propagating a
* {@link com.mongodb.DuplicateKeyException} or {@link com.mongodb.MongoException} on failure.
*/
Uni<InsertOneResult> insertOne(T document);
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param document the document to insert
* @param options the options to apply to the operation
* @return a {@link Uni} completed successfully when the operation completes, or propagating a
* {@link com.mongodb.DuplicateKeyException} or {@link com.mongodb.MongoException} on failure.
*/
Uni<InsertOneResult> insertOne(T document, InsertOneOptions options);
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param clientSession the client session with which to associate this operation
* @param document the document to insert
* @return a {@link Uni} completed successfully when the operation completes, or propagating a
* {@link com.mongodb.DuplicateKeyException} or {@link com.mongodb.MongoException} on failure.
*/
Uni<InsertOneResult> insertOne(ClientSession clientSession, T document);
/**
* Inserts the provided document. If the document is missing an identifier, the driver should generate one.
*
* @param clientSession the client session with which to associate this operation
* @param document the document to insert
* @param options the options to apply to the operation
* @return a {@link Uni} completed successfully when the operation completes, or propagating a
* {@link com.mongodb.DuplicateKeyException} or {@link com.mongodb.MongoException} on failure.
*/
Uni<InsertOneResult> insertOne(ClientSession clientSession, T document, InsertOneOptions options);