private void placeOrder(Message<JsonObject> msg) { mongo.save("orders", msg.body(), save -> { // error handling if (save.failed()) { msg.fail(500, save.cause().getMessage()); return; } msg.reply(new JsonObject()); }); }
private void loadData(MongoClient db) { db.dropCollection("users", drop -> { if (drop.failed()) { throw new RuntimeException(drop.cause()); } List<JsonObject> users = new LinkedList<>(); users.add(new JsonObject() .put("username", "pmlopes") .put("firstName", "Paulo") .put("lastName", "Lopes") .put("address", "The Netherlands")); users.add(new JsonObject() .put("username", "timfox") .put("firstName", "Tim") .put("lastName", "Fox") .put("address", "The Moon")); for (JsonObject user : users) { db.insert("users", user, res -> { System.out.println("inserted " + user.encode()); }); } }); } }
private void listAlbums(Message<JsonObject> msg) { // issue a find command to mongo to fetch all documents from the "albums" collection. mongo.find("albums", new JsonObject(), lookup -> { // error handling if (lookup.failed()) { msg.fail(500, lookup.cause().getMessage()); return; } // now convert the list to a JsonArray because it will be easier to encode the final object as the response. final JsonArray json = new JsonArray(); for (JsonObject o : lookup.result()) { json.add(o); } msg.reply(json); }); }
mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo")); mongo.find("users", new JsonObject(), lookup -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { JsonObject newUser = ctx.getBodyAsJson(); mongo.findOne("users", new JsonObject().put("username", newUser.getString("username")), null, lookup -> { mongo.insert("users", newUser, insert -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { user.put("address", update.getString("address")); mongo.replace("users", new JsonObject().put("_id", ctx.request().getParam("id")), user, replace -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { } else { mongo.remove("users", new JsonObject().put("_id", ctx.request().getParam("id")), remove -> {
.put("db_name", db); MongoClient mongoClient = MongoClient.createShared(vertx, mongoconfig); mongoClient.save("products", product1, id -> { System.out.println("Inserted id: " + id.result()); mongoClient.find("products", new JsonObject().put("itemId", "12345"), res -> { System.out.println("Name is " + res.result().get(0).getString("name")); mongoClient.remove("products", new JsonObject().put("itemId", "12345"), rs -> { if (rs.succeeded()) { System.out.println("Product removed ");
MongoClient mongo = MongoClient.createShared(vertx, config); Void v = awaitResult(h -> mongo.createCollection("users", h)); String id = awaitResult(h -> mongo.insert("users", user, h)); System.out.println("Inserted id is " + id); List<JsonObject> results = awaitResult(h -> mongo.find("users", new JsonObject(), h)); System.out.println("Retrieved " + results.size() + " results");
final MongoClient mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo")); mongo.find("users", new JsonObject(), lookup -> { mongo.insert("users", user, lookup -> { mongo.removeOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), lookup -> {
mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo"));
/** * Get a list of all collections in the database. * @param resultHandler will be called with a list of collections. * @return */ public io.vertx.rxjava.ext.mongo.MongoClient getCollections(Handler<AsyncResult<List<String>>> resultHandler) { delegate.getCollections(resultHandler); return this; }
/** * Drop a collection * @param collection the collection * @param resultHandler will be called when complete * @return */ public io.vertx.rxjava.ext.mongo.MongoClient dropCollection(String collection, Handler<AsyncResult<Void>> resultHandler) { delegate.dropCollection(collection, resultHandler); return this; }
/** * Creates an index. * @param collection the collection * @param key A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1. * @param resultHandler will be called when complete * @return */ public io.vertx.rxjava.ext.mongo.MongoClient createIndex(String collection, JsonObject key, Handler<AsyncResult<Void>> resultHandler) { delegate.createIndex(collection, key, resultHandler); return this; }
/** * Find matching documents in the specified collection, specifying options * @param collection the collection * @param query query used to match documents * @param options options to configure the find * @param resultHandler will be provided with list of documents * @return */ public io.vertx.rxjava.ext.mongo.MongoClient findWithOptions(String collection, JsonObject query, FindOptions options, Handler<AsyncResult<List<JsonObject>>> resultHandler) { delegate.findWithOptions(collection, query, options, resultHandler); return this; }
/** * Run an arbitrary MongoDB command. * @param commandName the name of the command * @param command the command * @param resultHandler will be called with the result. * @return */ public io.vertx.rxjava.ext.mongo.MongoClient runCommand(String commandName, JsonObject command, Handler<AsyncResult<JsonObject>> resultHandler) { delegate.runCommand(commandName, command, resultHandler); return this; }
/** * Update matching documents in the specified collection and return the handler with MongoClientUpdateResult result * @param collection the collection * @param query query used to match the documents * @param update used to describe how the documents will be updated * @param resultHandler will be called when complete * @return */ public io.vertx.rxjava.ext.mongo.MongoClient updateCollection(String collection, JsonObject query, JsonObject update, Handler<AsyncResult<MongoClientUpdateResult>> resultHandler) { delegate.updateCollection(collection, query, update, resultHandler); return this; }
/** * Execute a bulk operation. Can insert, update, replace, and/or delete multiple documents with one request. * @param collection the collection * @param operations the operations to execute * @param resultHandler will be called with a {@link io.vertx.ext.mongo.MongoClientBulkWriteResult} when complete * @return */ public io.vertx.rxjava.ext.mongo.MongoClient bulkWrite(String collection, List<BulkOperation> operations, Handler<AsyncResult<MongoClientBulkWriteResult>> resultHandler) { delegate.bulkWrite(collection, operations, resultHandler); return this; }
/** * Remove matching documents from a collection and return the handler with MongoClientDeleteResult result * @param collection the collection * @param query query used to match documents * @param resultHandler will be called when complete * @return */ public io.vertx.rxjava.ext.mongo.MongoClient removeDocuments(String collection, JsonObject query, Handler<AsyncResult<MongoClientDeleteResult>> resultHandler) { delegate.removeDocuments(collection, query, resultHandler); return this; }
/** * Get all the indexes in this collection. * @param collection the collection * @param resultHandler will be called when complete * @return */ public io.vertx.rxjava.ext.mongo.MongoClient listIndexes(String collection, Handler<AsyncResult<JsonArray>> resultHandler) { delegate.listIndexes(collection, resultHandler); return this; }
private void loadData(MongoClient db) { db.dropCollection("albums", drop -> { if (drop.failed()) { throw new RuntimeException(drop.cause()); db.insert("albums", album, res -> { System.out.println("inserted " + album.encode()); });
/** * Find matching documents in the specified collection * @param collection the collection * @param query query used to match documents * @param resultHandler will be provided with list of documents * @return */ public io.vertx.rxjava.ext.mongo.MongoClient find(String collection, JsonObject query, Handler<AsyncResult<List<JsonObject>>> resultHandler) { delegate.find(collection, query, resultHandler); return this; }
@Override public MongoService createIndex(String collection, JsonObject key, Handler<AsyncResult<Void>> resultHandler) { client.createIndex(collection, key, resultHandler); return this; }