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.find("users", new JsonObject(), lookup -> {
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"));
List<JsonObject> results = awaitResult(h -> mongo.find("users", new JsonObject(), h)); System.out.println("Retrieved " + results.size() + " results");
mongo.find("users", new JsonObject(), lookup -> {
/** * 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 find(String collection, JsonObject query, Handler<AsyncResult<List<JsonObject>>> resultHandler) { client.find(collection, query, resultHandler); return this; }
/** * 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; }
public void getPlacesForCategory(String category, Handler<AsyncResult<List<Place>>> resultHandler) { mongo.find(COLLECTION, new JsonObject().put("category", category), ar -> { if (ar.failed()) { resultHandler.handle(Future.failedFuture(ar.cause())); } else { List<Place> places = ar.result().stream() .map(Place::new).collect(Collectors.toList()); resultHandler.handle(Future.succeededFuture(places)); } } ); }
private void getAll(RoutingContext routingContext) { mongo.find(COLLECTION, new JsonObject(), results -> { List<JsonObject> objects = results.result(); List<Whisky> whiskies = objects.stream().map(Whisky::new).collect(Collectors.toList()); routingContext.response() .putHeader("content-type", "application/json; charset=utf-8") .end(Json.encodePrettily(whiskies)); }); }
public static io.vertx.ext.mongo.MongoClient find(io.vertx.ext.mongo.MongoClient j_receiver, java.lang.String collection, java.util.Map<String, Object> query, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.List<java.util.Map<String, Object>>>> resultHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.find(collection, query != null ? io.vertx.core.impl.ConversionHelper.toJsonObject(query) : null, resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.List<io.vertx.core.json.JsonObject>>>() { public void handle(io.vertx.core.AsyncResult<java.util.List<io.vertx.core.json.JsonObject>> ar) { resultHandler.handle(ar.map(event -> event != null ? event.stream().map(elt -> io.vertx.core.impl.ConversionHelper.fromJsonObject(elt)).collect(java.util.stream.Collectors.toList()) : null)); } } : null)); return j_receiver; } public static io.vertx.core.streams.ReadStream<io.vertx.core.json.JsonObject> findBatch(io.vertx.ext.mongo.MongoClient j_receiver, java.lang.String collection, java.util.Map<String, Object> query) {
@Override public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) { String username = authInfo.getString(this.usernameCredentialField); String password = authInfo.getString(this.passwordCredentialField); // Null username is invalid if (username == null) { resultHandler.handle((Future.failedFuture("Username must be set for authentication."))); return; } if (password == null) { resultHandler.handle((Future.failedFuture("Password must be set for authentication."))); return; } AuthToken token = new AuthToken(username, password); JsonObject query = createQuery(username); mongoClient.find(this.collectionName, query, res -> { try { if (res.succeeded()) { User user = handleSelection(res, token); resultHandler.handle(Future.succeededFuture(user)); } else { resultHandler.handle(Future.failedFuture(res.cause())); } } catch (Throwable e) { log.warn(e); resultHandler.handle(Future.failedFuture(e)); } }); }
public void getAll(Class<T> clazz, Handler<ExtendedAsyncResult<List<T>>> fut) { final String q = "{}"; JsonObject jq = new JsonObject(q); cli.find(collection, jq, res -> { if (res.failed()) { fut.handle(new Failure<>(INTERNAL, res.cause())); } else { List<JsonObject> resl = res.result(); List<T> ml = new LinkedList<>(); for (JsonObject jo : resl) { decode(jo); T env = Json.decodeValue(jo.encode(), clazz); ml.add(env); } fut.handle(new Success<>(ml)); } }); }
@Override public void updateModules(String id, SortedMap<String, Boolean> enabled, Handler<ExtendedAsyncResult<Void>> fut) { JsonObject jq = new JsonObject().put("_id", id); cli.find(COLLECTION, jq, gres -> { if (gres.failed()) { logger.debug("disableModule: find failed: " + gres.cause().getMessage()); fut.handle(new Failure<>(INTERNAL, gres.cause())); } else { List<JsonObject> l = gres.result(); if (l.isEmpty()) { logger.debug("disableModule: not found: " + id); fut.handle(new Failure<>(NOT_FOUND, messages.getMessage("1100", id))); } else { JsonObject d = l.get(0); final Tenant t = decodeTenant(d); t.setEnabled(enabled); JsonObject document = encodeTenant(t, id); cli.save(COLLECTION, document, sres -> { if (sres.failed()) { logger.debug("TenantStoreMongo: disable: saving failed: " + sres.cause().getMessage()); fut.handle(new Failure<>(INTERNAL, sres.cause())); } else { fut.handle(new Success<>()); } }); } } }); }
final String id = td.getId(); JsonObject jq = new JsonObject().put("_id", id); cli.find(COLLECTION, jq, res -> { if (res.failed()) {
private boolean verifyUserData() throws Exception { final StringBuffer buffer = new StringBuffer(); CountDownLatch intLatch = new CountDownLatch(1); String collectionName = authProvider.getCollectionName(); log.info("verifyUserData in " + collectionName); getMongoClient().find(collectionName, new JsonObject(), res -> { if (res.succeeded()) { log.info(res.result().size() + " users found: " + res.result()); } else { log.error("", res.cause()); buffer.append("false"); } intLatch.countDown(); }); awaitLatch(intLatch); return buffer.length() == 0; }