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?
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.
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.
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.
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
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)>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With