10
votes

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?

2
Does db.col1.save(doc) work? Or db.testaj.save(doc)? - Miguel-F
I can't see where you are creating the col1 variable in your code. Shouldn't it be: col1 = db.getCollection("col1"); col1.insert(doc) ? - John Whish
hi John.. here it is :<cfset db.createCollection("col1") > - user3569267
Hi Miguel. No the 2 syntaxe does not work. It generates the following error: (No matching property [TESTAJ] (or col1) found in [com.mongodb.MongoDatabaseImpl]) - user3569267
John, do you know for which version of driver the syntaxe col1 = db.getCollection("col1"); col1.insert(doc) works? - user3569267

2 Answers

2
votes

Hopefully you have found the answer by now since this ticket is now 5 months old but just for reference:

To insert a new document you should use col1.insertOne(doc) or col1.insertMany(docs) (if you have an array)

To update a document you should use col1.updateOne(query, values) or col1.updatedMany(query, values)

As mentioned by Leigh more information about this can be found at: http://mongodb.github.io/mongo-java-driver/3.2/driver/getting-started/quick-tour/#mongodb-driver-quick-tour

2
votes

I'm using MongoDB 4.0, Mongo-Java-Driver 3.8.0 with CF11. After trial and error, I made the code below work. The key was converting to org.bson.document. Hopefully it saves someone else some time.

<cfset Mongo  = CreateObject("java","com.mongodb.MongoClient").init("localhost")>
<cfset db = Mongo.getDatabase('testDB')>
<cfset testCol = db.getCollection("testCol")>

<cfscript>
x=structnew();
x.name="John Doe";
x.address="1010 Yellow Brick Road";
x.phone="867-5309"
</cfscript>

<cfset data=CreateObject("java","org.bson.Document").init(x)>
<cfset testCol.insertOne(data)>