Search code examples
mongodbcoldfusioncoldfusion-2016coldfusion-2018mongodb4.0

ColdFusion 2016 and MongoDB 4.0.13


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}}]


Solution

  • 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.