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
Related
I try to connect to a MongoDB Atlas database through mongo_dart with this line of code. The provided link from MongoDB is defined by:
mongodb+srv://<user>:<PASSWORD>#test-asdf.mongodb.net/test?retryWrites=true
throws an "Invalid scheme" Error. When I cut out "+srv" and try to connect with:
Db db = new Db("mongodb://<user>:<password>#test-asdf.mongodb.net/test?retryWrites=true");
it throws a SocketException: Failed host lookup.
Is it even possible to access to a atlas mongoDB or am I forgetting something?
The mongodb+srv:// protocol is for new driver, maybe you can try to click the button "I am using driver 3.4 or earlier" to get the legacy url with mongodb:// protocol
In order to connect to atlas, you need to pass connection string which connect your atlas to mongo_dart like this:
import "package:mongo_dart/mongo_dart.dart;
void getConnection() async {
String connectionString = "mongodb+srv://<user>:<password>#test-asdf.mongodb.net/test?retryWrites=true&w=majority";
print(connectionString);
// Connect to database:
Db db = await Db.create(connectionString);
await db.open();
print(db);
}
I am trying to connect MongoDB using MongoClientURI(URL) my URL is mongodb://userName:Password#host:PortNumber/DBName?connectTimeoutMS=10000
when my MongoDB is Down i try to Post Request but it take default time 30 sec.
Can any one help me solve the problem
Thanks in Advance.
You can set timeouts by using the Mongo Java client's MongoClientOptions. For example:
MongoClientOptions clientOptions = MongoClientOptions.builder()
.connectTimeout(...)
.socketTimeout(...)
.serverSelectionTimeout(...)
.build();
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), clientOptions);
Examining mongoClient.getMongoClientOptions() after the above line of code clearly shows that the created client is faithful to the supplied config values. By contrast, if you do not set these values via MongoClientOptions then mongoClient.getMongoClientOptions() shows that the default values have been chosen.
Based on your updated comments I think the situation you are trying to cater for is this:
Creating a connection against a server instance which does not exists / is unavailable should fail sooner that the default of 30s.
If so then the configuration parameter you want to use is serverSelectionTimeout. The following invocation ...
MongoClientOptions clientOptions = MongoClientOptions.builder()
.serverSelectionTimeout(2000)
.build();
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), clientOptions);
... will cause this exception to be thrown:
com.mongodb.MongoTimeoutException: Timed out after 2000 ms while waiting to connect.
Note: serverSelectionTimeout is available in the version of the MongoDB Java driver which you are using (3.2.2 according to the comment you posted on your question).
I do have a database named users but pymongo is looking for a db named as app is there any way to change to Pymongo connection method ?
this one is working if i crate a db named as app;
app = Flask(__name__)
mongo = PyMongo(app)
i want to connect like this but i do get an error for that ;
mongo = PyMongo('users')
I have found my answer;
client = MongoClient('localhost')
db = client['dbname']
collection = db.collection_name
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'dbname'
mongo = PyMongo(app)
This allows you to use flask_pymongo, more info here
I have made changes to source code, I require to run all testcases to check its effect using command
./gradlew check
I am having the mongodb running in remote machine. Can anyone help me in configuring java mongodb driver with remotely running mongodb.
you need to import the java driver to your project.
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
then you need to connect to the mongoDB on your server, it can be localhost or it can be your server. also you can choose which port to use:
MongoClient mongoClient = new MongoClient("localhost", 27017);
then you can connect to your db:
MongoDatabase db = mongoClient.getDatabase("test");
and to connect to one of your collections, and make actions on it:
db.getCollection("restaurants").insertOne(
new Document("address",
new Document()
.append("street", "2 Avenue")
.append("zipcode", "10075")
.append("building", "1480")
.append("coord", asList(-73.9557413, 40.7720266)))
.append("borough", "Manhattan")
.append("cuisine", "Italian")
.append("grades", asList(
new Document()
.append("grade", "A")
.append("score", 11),
new Document()
.append("grade", "B")
.append("score", 17)))
.append("name", "Vella")
.append("restaurant_id", "41704620"));
One can pass the connection string while starting testcases
./gradlew check -Dorg.mongodb.test.uri=mongodb://example.com:27017/
I have a small connection routine which is as below:
val dbName = "myDb"
val user = "myUser"
val pass = "myPass"
val mongoDriver = new reactivemongo.api.MongoDriver()
val db = MyDBObject(
mongoDriver.connection(
Seq("XXXXX.mongolab.com:XXXXX"),
options = MongoConnectionOptions(authMode = ScramSha1Authentication),
authentications = List(Authenticate(dbName, user, pass))
),
dbName
)
I'm using this to create a new connection and then using this connection, I work with my documents. But the problem is that for some very strange reason, I could not get this to work!
Here is the error that I get:
CommandError[code=13, errmsg=not authorized on myDb to execute command {....,
code: BSONInteger(13)
}]
I've been trying to dig into this for almost 3 hours without any vail! The MongoLab uses 3.0 version of MongoDB and I use 0.11.7 version for the ReactiveMongo library.
Using the mongo shell, I'm able to log in to the MongoLab and create new collections with the same set of credentials!