我正在使用MongoDB 3.2.3和mongo-java-driver-3.2.2.jar庫。如何在ColdFusion中使用MongoDB
我做了以下爲了連接到服務器,然後到特定的數據庫。然後,我創建了一個名爲col1
集合:
<cfset Mongo = CreateObject("java","com.mongodb.MongoClient")>
<cfset Mongo.init("192.168.0.30")>
<cfset db = Mongo.getDatabase('testaj')>
<cfset db.createCollection("col1") >
我從互聯網上,以將文檔插入集合下面的代碼:
<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)>
不幸的是,最後一行生成以下錯誤:
No matching Method/Function for com.mongodb.MongoCollectionImpl.save(com.mongodb.BasicDBObject) found
這似乎是完全合乎邏輯的,因爲當我輸出(通過cfdump
)類的內容3210,我沒有看到方法save
。
有誰知道如何使用此Java驅動程序在ColdFusion中插入MongoDB文檔?
我應該導入另一個圖書館或班級嗎?
'db.col1.save(doc)'工作嗎?或'db.testaj.save(doc)'? –
我看不到你在代碼中創建'col1'變量的地方。它不應該是:'col1 = db.getCollection(「col1」); col1.insert(doc)'? –
嗨,約翰..這裏是: –
user3569267