I am using MongoDB 3.2.3 and the mongo-java-driver-3.2.2.jar library.
I did the following in order to connect to the server and then to a specific database. I then create a collection named col1:
<cfset Mongo = CreateObject("java","com.mongodb.MongoClient")>
<cfset Mongo.init("192.168.0.30")>
<cfset db = Mongo.getDatabase('testaj')>
<cfset db.createCollection("col1") >
I got the following code from the internet in order to insert a document into the collection:
<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 doc = {
"Name" = "Marc",
"Spouse"= "Heather",
"Fruit" = "Mango",
"Kids" = [
{"Name"="Alexis", "Age"=7, "Hair"="blonde", "Description"="crazy" },
{"Name"="Sidney", "Age"=2, "Hair"="dirty blonde", "Description"="ornery" }
],
"Bike" = "Felt",
"LoveSQL" = true,
"TS" = now(),
"Counter" = 1
}>
<cfset doc = SerializeJSON(doc)>
<cfset doc = m(doc)>
<cfset col1.save(doc)>
Unfortunately the last line generates the following error:
No matching Method/Function for com.mongodb.MongoCollectionImpl.save(com.mongodb.BasicDBObject) found
It seems to be completely logical because when I output (via a cfdump) the content of the class com.mongodb.MongoCollectionImpl, I don't see the method save.
Does anyone know how to insert a MongoDB document in ColdFusion using this Java driver?
Should I import another library or class?
db.col1.save(doc)work? Ordb.testaj.save(doc)? - Miguel-Fcol1variable in your code. Shouldn't it be:col1 = db.getCollection("col1"); col1.insert(doc)? - John Whish