D10078 - Vert.x Native, MongoClient

For mongo db configuration you could refer previous tutorials D10077 - Reference, Mongo Setup to check more details, now this chapter we’ll go back to zero system to see how to enable vert.x native client for mongo db.

Demo projects:

1. Configuration

This chapter is duplicated with D10073 - Configuration, vertx-mongo.yml, because that’s the preparing steps for mysql client.

1.1. vertx.yml

  lime: mysql, mongo
    - name: vertx-zeus
        maxEventLoopExecuteTime: 30000000000

1.2. vertx-inject.yml

mongo: io.vertx.tp.plugin.mongo.MongoInfix

1.3. vertx-mongo.yml

All the mongo configurations are put into vertx-mongo.yml the root node mongo as following:

  db_name: ZERO_MESH
  port: 6017
  connection_string: mongodb://localhost:6017
  username: zero_mongo
  password: zero_mongo

All above configuration is for mongo native configuration supported, but for up-gaea project, there need some additional configuration ( Server Config ).

2. Server Configuration


- name: up-gaea
  type: http
    port: 6073

Finally when you have finished configuration, the folder structure of project should be as following:

3. Source Code

3.1. Api

package up.god.micro.mongo;

import io.vertx.core.json.JsonObject;
import io.vertx.up.annotations.Address;
import io.vertx.up.annotations.EndPoint;

import javax.ws.rs.BodyParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

public interface MongoApi {

    JsonObject sayMongo(@BodyParam JsonObject params);


package up.god.micro.mongo;

import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.mongo.MongoClient;
import io.vertx.up.unity.Ux;
import io.vertx.up.annotations.Address;
import io.vertx.up.annotations.Queue;
import io.vertx.up.commune.Envelop;

import javax.inject.infix.Mongo;

public class MongoWorker {

    private transient MongoClient client;

    public void sayMongo(final Message<Envelop> message) {
        final JsonObject data = Ux.getJson(message);
        this.client.insert("DB_TEST", data, res -> {
            if (res.succeeded()) {
            } else {

4. Console

When you started the application you should see following output:

Cluster created with settings {hosts=[localhost:6017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Opened connection [connectionId{localValue:1, serverValue:5}] to localhost:6017
Monitor thread successfully connected to server with description ServerDescription{address=localhost:6017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 2]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=5597013}

5. Testing

Then you could test with postman

URL : http://localhost:6073/api/native/test

Method : POST

Request :


Response :

    "data": {
        "username": "Lang",
        "password": "pl,okmijnuhb123",
        "email": "lang.yu@email.com",
        "_id": "5a8011506b0f4bc139b3f6ce"

6. Summary

Here this record has been added into mongo database, you can check the result from mongo db:

The record is you added in just request.