Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MongoDB with ColdFusion

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?

like image 421
user3569267 Avatar asked Mar 30 '16 12:03

user3569267


People also ask

How does MongoDB compass work?

MongoDB Compass is a convenient tool for browsing through the data stored in a MongoDB database through a graphical interface. It removes the burden of having to remember the names of obscure databases or collections, and you can navigate to any database or collection on your MongoDB server with just a few clicks.

How do I create a collection in MongoDB?

MongoDB creates collections automatically when you insert some documents. For example: Insert a document named seomount into a collection named SSSIT. The operation will create the collection if the collection does not currently exist. If you want to see the inserted document, use the find() command.

When should I use MongoDB?

NoSQL databases like MongoDB are a good choice when your data is document-centric and doesn't fit well into the schema of a relational database, when you need to accommodate massive scale, when you are rapidly prototyping, and a few other use cases.


2 Answers

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

like image 160
rdubya Avatar answered Oct 04 '22 01:10

rdubya


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)>
like image 22
steve Avatar answered Oct 04 '22 01:10

steve