1

I am trying to connect ColdFusion 2016 (local machine developer mode) to MongoDB 4.0.13 (server). I installed mongodb-driver-core-3.8.2.jar, bson-3.8.2.jar and mongodb-driver-3.8.2.jar into my lib folder. when I try to run this code, it never connects to Mongo, it errs out. Am I not using correct drivers ?

Code:

<cfset uri  = CreateObject("java","com.mongodb.MongoClientURI").init("mongodb://wh-mongos-v01.shift4.com:27017")>
<cfset mongoClient  = CreateObject("java","com.mongodb.MongoClient").init(uri)>

<cffunction name="m" returntype="any">
    <cfargument name="value" type="any">
    <cfif IsJSON(arguments.value)>
        <cfset local.retrun = CreateObject("java","com.mongodb.util.JSON").parse(arguments.value)>
    <cfelse>
        <cfset local.retrun = CreateObject("java","com.mongodb.util.JSON").parse( SerializeJSON(arguments.value) )>
    </cfif>
    <cfreturn local.retrun>
</cffunction>

<cfset myDb = mongoClient.getDatabase("testingdb")>
<cfset myCollection = myDb.getCollection("testingcollection")>
<cfdump var="#myCollection.countDocuments()#">

Error: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=wh-mongodb-v01.xxxxx.com:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]

2 Answers2

1

I figured it out. Here is what it needs:

  • CF needs only Mongo Legacy driver. so I checked the compatibility matrix and loaded up 3.12.1 the latest uber legacy driver.
  • The trouble was with SSL so it Mongo has SSL turned on we need to use the SSL=True option.

0

Do you have a MongoDB node running at wh-mongodb-v01.xxxxx.com on port 27017? In any case, the best option is to use a Connection URI string where you specify multiple replica set nodes for high availability. In that case your connection should be established with:

<cfset uri = CreateObject("java","com.mongodb.MongoClientURI").init("mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]/)>
<cfset mongoClient = CreateObject("java","com.mongodb.MongoClient").init(uri)>
Nic Cottrell
  • 9,401
  • 7
  • 53
  • 76
  • Thanks for the response, Nic, it didn't work either. I am able to telnet to the node on 27017. MongoDB is open and there are no username or passwords setup. – Ashok Ramkumar Jan 23 '20 at 23:00
  • Also Nic, Can you advise on what all the JARs needed for it ? I just have the legacy Java JAR installed. – Ashok Ramkumar Jan 23 '20 at 23:05
  • The 3.8.2 Java driver should work with server 4.0, but I'd recommend upgrading to 3.12.2 and put that JARs in your CF classpath – Nic Cottrell Jan 28 '20 at 13:59