1

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?

Jules
  • 546
  • 2
  • 11
  • 36

1 Answers1

0

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

imTachu
  • 3,759
  • 4
  • 29
  • 56