Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--- Creating MongoCliente object --->
- <cfset Mongo = CreateObject("java","com.mongodb.MongoClient")>
- <!--- Connecting to database, if not exists then create --->
- <cfset db = Mongo.getDB('coldcocoon')>
- <!--- Choosing collection if not exists then create --->
- <cfset document = db.getCollection('document')>
- <!--- To handle ObjectID --->
- <cfset ObjectID = CreateObject("java","org.bson.types.ObjectId")>
- <!--- parseJSON to parse --->
- <cffunction name="parseJSON" 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>
- <!--- --->
- <!--- CRUD OPERATIONS --->
- <!--- Create Function --->
- <cffunction name="create" returntype="string">
- <cfargument name ="value" type="any">
- <cfset doc = SerializeJSON(value)>
- <cfset doc = parseJSON(doc)>
- <cfset document.save( doc )>
- <cfreturn "done">
- </cffunction>
- <!--- Update Function --->
- <cffunction name="updateByID" returntype="any" access="remote" returnFormat="json">
- <cfargument name ="id" type="any">
- <cfargument name ="modification" type="any">
- <cfset query = { "_id" = {"$oid" = "#id#"}} >
- <cfset query = parseJSON(query) >
- <cfset modificationFields = {"$set" = #modification#}>
- <cfset doc = SerializeJSON(modificationFields)>
- <cfset doc = parseJSON(doc)>
- <cfset document.findAndModify(query, doc)>
- <cfreturn "done">
- </cffunction>
- <!--- Read Functions --->
- <cffunction name="getById" returntype="any" access="remote" returnFormat="json">
- <cfargument name ="id" type="any">
- <cfset response = document.findOne(ObjectID.init(id))>
- <cfreturn response>
- </cffunction>
- <!--- Destroy Function --->
- <cffunction name="delete" returntype="void" access="remote" returnFormat="json">
- <cfargument name ="id" type="any">
- <cfset query = { "_id" = {"$oid" = "#id#"}} >
- <cfset document.findAndRemove(parseJSON(query))>
- </cffunction>
- <!--- Using CRUD Operations --->
- <cfset example = {
- "name" = "Foo",
- "age" = 26,
- "cancel" = true,
- "createdAt" = now(),
- "nested" = {
- "value1" = 5,
- "value2" = "abcdef...",
- "value3" = true
- }
- }>
- <!--- <cfset create(example)> --->
- <cfset values = {
- "cancel" = false
- }>
- <cfset updateById(id="5720e5eda8d1e019284fb315", modification=values)>
- <!---<cfset select = getById("571e5d84a8d1e0249066aac5")>
- <cfoutput>
- #select['age']#
- </cfoutput>--->
- <cfset delete("571e5d84a8d1e0249066aac5")>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement