B4J Question MongoDB - exception when connecting

lte5000

Member
Licensed User
Longtime User
I am trying to connect to my MongoDB database hosted on mLab.

I have double checked that the username/password and the URI are correct, and that they do work successfully from the command line.

This is the code I am using (sensitive info deleted):
B4X:
mongo.Initialize("", "mongodb://username:password@ds******-a1.mlab.com:*****")
db = mongo.GetDatabase("example")
ranks = db.GetCollection("example")
Dim res As List = ranks.Find(CreateMap("_id": "example"), Null, Null)

I am getting the error/exception attached below... would appreciate any help in deciphering the error.

Thanks!

B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 44 (Main)
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=ds******-a1.mlab.com:*****, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='*******', source='admin', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandE...
   at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:375)
   at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104)
   at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
   at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
   at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
   at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
   at com.mongodb.operation.FindOperation.execute(FindOperation.java:483)
   at com.mongodb.operation.FindOperation.execute(FindOperation.java:80)
   at com.mongodb.Mongo.execute(Mongo.java:818)
   at com.mongodb.Mongo$2.execute(Mongo.java:805)
   at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
   at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:143)
   at com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:36)
   at anywheresoftware.mongo.MongoUtils.ListFromIterable(MongoUtils.java:14)
   at anywheresoftware.mongo.MongoCollectionWrapper.Find2(MongoCollectionWrapper.java:82)
   at b4j.example.main._rankbetween(main.java:86)
   at b4j.example.main._appstart(main.java:71)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:612)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:226)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
   at b4j.example.main.main(main.java:29)
Program terminated (StartMessageLoop was not called).
 

lte5000

Member
Licensed User
Longtime User
The problem turned out to be simple... I was missing the database name at the end of the URI.

The correct and working mLab URI format is:

B4X:
mongo.Initialize("", "mongodb://username:password@ds******-a1.mlab.com/<database-name>:*****")
 
Upvote 0
Top