简体   繁体   中英

Jongo connect to remote MongoDB server

Is it possible to connect to a remote MongoDB when using Jongo ( jongo.org )?

I saw a piece of code where MongoClientURI was used like this:

MongoClientURI uri = new MongoClientURI("mongodb://IP_ADDRESS:27017/DB_NAME");

I have the following code:

if(client != null) {
        db = client.getDatabase("StockApp");
        database = client.getDB("StockApp");
        jongo = new Jongo(database);

    }

In this example, StockApp is the name of my database. It will connect to my local database (127.0.0.1:27017/StockApp). When I try to change StockApp to uri.getDatabase() in both lines, I get the following exception:

com.mongodb.MongoSocketOpenException: Exception opening socket

I can also see that it tries to connect to localhost (127.0.0.1).

When I change the uri to new MongoClientURI("IP_ADDRESS") or new MongoClientURI("IP_ADDRESS:27017) I get the error that the uri should start with mongodb://

Does anyone know if it is possible to connect to a remote MongoDB server using Jongo?

You can initialize Jongo from a MongoClient like this:

MongoClient mongoClient = new MongoClient("host", 27017);
DB db = mongoClient.getDB("theDB");
Jongo jongo = new Jongo(db);

You can check the MongoClient constructor detail here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM