List of usage examples for com.mongodb.async.client MongoCollection insertOne
void insertOne(TDocument document, SingleResultCallback<Void> callback);
From source file:beans.PointsImpl.java
License:Apache License
@Override public CompletionStage<Void> addPoint(Point point) { MongoCollection<Document> collection = mongoClient.getDatabase(dbName).getCollection(dbCollection); CompletableFuture<Void> future = new CompletableFuture<>(); collection.insertOne(RecordConverter.toDocument(point), getSingleResultCallback(future)); return future; }
From source file:com.example.App.java
License:Apache License
public static void main(String[] args) throws InterruptedException { final MongoClientSettings settings = MongoClientSettings.builder() .addCommandListener(new CommandListener() { @Override//from w w w . j a va2 s . c o m public void commandStarted(CommandStartedEvent event) { logger.info("command: db = {}, command = {}", event.getDatabaseName(), event.getCommandName()); } @Override public void commandSucceeded(CommandSucceededEvent event) { logger.info("command succeed: request = {}, command = {}", event.getRequestId(), event.getCommandName()); } @Override public void commandFailed(CommandFailedEvent event) { logger.info("command failed: request = {}, command = {}", event.getRequestId(), event.getCommandName()); logger.error("detail", event.getThrowable()); } }).applicationName("sample-app") .applyToConnectionPoolSettings(builder -> builder.maxSize(1).minSize(1)).build(); final MongoClient client = MongoClients.create(settings); final MongoDatabase database = client.getDatabase("sample"); final MongoCollection<Document> collection = database.getCollection("test"); final Document firstDocument = new Document("id", UUID.randomUUID()).append("name", "test user") .append("created", LocalDateTime.now()); logger.info("document to be saved: {}", firstDocument); final Mono<Document> firstMono = Mono.create(sink -> collection.insertOne(firstDocument, (result, t) -> { if (t == null) { logger.info("inserted: {}", firstDocument); sink.success(firstDocument); } else { logger.error("error", t); sink.error(t); } })); final Mono<List<Document>> secondMono = create100Users(collection, firstMono); final Mono<Long> thirdMono = secondMono.then(Mono.create(sink -> collection.countDocuments((count, t) -> { if (t == null) { logger.info("collection has {} items.", count); sink.success(count); } else { logger.error("error", t); sink.error(t); } }))); final Mono<List<Document>> fourthMono = create100Users(collection, thirdMono); final Mono<List<Document>> fifthMono = create100Users(collection, fourthMono); final Mono<List<Document>> sixthMono = create100Users(collection, fifthMono); final Mono<Document> seventhMono = sixthMono.then(Mono.create(sink -> collection.find().first((doc, t) -> { if (t == null) { logger.info("found document: {}", doc); sink.success(doc); } else { logger.error("error", t); sink.error(t); } }))); final CountDownLatch latch = new CountDownLatch(1); seventhMono.doOnTerminate(() -> { latch.countDown(); client.close(); }).subscribe(doc -> logger.info("first document: {}", doc)); latch.await(); }
From source file:io.vertx.ext.mongo.impl.MongoClientImpl.java
License:Open Source License
@Override public io.vertx.ext.mongo.MongoClient saveWithOptions(String collection, JsonObject document, WriteOption writeOption, Handler<AsyncResult<String>> resultHandler) { requireNonNull(collection, "collection cannot be null"); requireNonNull(document, "document cannot be null"); requireNonNull(resultHandler, "resultHandler cannot be null"); MongoCollection<JsonObject> coll = getCollection(collection, writeOption); Object id = document.getValue(ID_FIELD); if (id == null) { coll.insertOne(document, convertCallback(resultHandler, wr -> useObjectId ? document.getJsonObject(ID_FIELD).getString(JsonObjectCodec.OID_FIELD) : document.getString(ID_FIELD))); } else {/* w w w . jav a2 s . co m*/ JsonObject filter = new JsonObject(); JsonObject encodedDocument = encodeKeyWhenUseObjectId(document); filter.put(ID_FIELD, encodedDocument.getValue(ID_FIELD)); com.mongodb.client.model.UpdateOptions updateOptions = new com.mongodb.client.model.UpdateOptions() .upsert(true); coll.replaceOne(wrap(filter), encodedDocument, updateOptions, convertCallback(resultHandler, result -> null)); } return this; }
From source file:io.vertx.ext.mongo.impl.MongoClientImpl.java
License:Open Source License
@Override public io.vertx.ext.mongo.MongoClient insertWithOptions(String collection, JsonObject document, WriteOption writeOption, Handler<AsyncResult<String>> resultHandler) { requireNonNull(collection, "collection cannot be null"); requireNonNull(document, "document cannot be null"); requireNonNull(resultHandler, "resultHandler cannot be null"); JsonObject encodedDocument = encodeKeyWhenUseObjectId(document); boolean hasCustomId = document.containsKey(ID_FIELD); MongoCollection<JsonObject> coll = getCollection(collection, writeOption); coll.insertOne(encodedDocument, convertCallback(resultHandler, wr -> { if (hasCustomId) return null; JsonObject decodedDocument = decodeKeyWhenUseObjectId(encodedDocument); return decodedDocument.getString(ID_FIELD); }));/* w w w . j a v a 2 s .c o m*/ return this; }
From source file:mongodb.clients.percunia.mongo.AsyncClient.java
License:Apache License
@Override public void createDocument(final String collectionName, Object object, final ResultCallback callback) { MongoCollection<Document> collection = database.getCollection(collectionName); try {//from w w w . j ava2 s . c om String json = mapper.writeValueAsString(object); Document doc = Document.parse(json); collection.insertOne(doc, new SingleResultCallback<Void>() { @Override public void onResult(final Void result, final Throwable t) { onInsertResultAction(t, "success", callback); } }); } catch (JsonProcessingException e) { e.printStackTrace(); callback.onError(e.getMessage()); } }
From source file:org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManagerAsync.java
License:Open Source License
private void save(DocumentEntity entity, SingleResultCallback<Void> callBack) { String collectionName = entity.getName(); com.mongodb.async.client.MongoCollection<Document> collectionAsync = asyncMongoDatabase .getCollection(collectionName); Document document = getDocument(entity); collectionAsync.insertOne(document, callBack); }