ColdFusion 2016 and MongoDB 4.0.13

280 views Asked by At

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

There are 2 answers

0
Ashok Ramkumar On BEST ANSWER

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.

3
Nic Cottrell On

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)>