Advertisement
nestor94

Untitled

Apr 27th, 2016
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!--- Creating MongoCliente object --->
  2. <cfset Mongo  = CreateObject("java","com.mongodb.MongoClient")>
  3. <!--- Connecting to database, if not exists then create  --->
  4. <cfset db = Mongo.getDB('coldcocoon')>
  5. <!--- Choosing collection if not exists then create --->
  6. <cfset document = db.getCollection('document')>
  7. <!--- To handle ObjectID --->
  8. <cfset ObjectID = CreateObject("java","org.bson.types.ObjectId")>
  9. <!--- parseJSON to parse --->
  10. <cffunction name="parseJSON" returntype="any">
  11.     <cfargument name="value" type="any">
  12.     <cfif IsJSON(arguments.value)>
  13.         <cfset local.retrun = CreateObject("java","com.mongodb.util.JSON").parse(arguments.value)>
  14.     <cfelse>
  15.         <cfset local.retrun = CreateObject("java","com.mongodb.util.JSON").parse( SerializeJSON(arguments.value) )>    
  16.     </cfif>
  17.     <cfreturn local.retrun>
  18. </cffunction>
  19.  
  20. <!---  --->
  21. <!--- CRUD OPERATIONS --->
  22. <!--- Create Function --->
  23. <cffunction name="create" returntype="string">
  24.     <cfargument name ="value" type="any">
  25.     <cfset doc = SerializeJSON(value)>
  26.     <cfset doc = parseJSON(doc)>
  27.     <cfset document.save( doc )>
  28.     <cfreturn "done">
  29. </cffunction>
  30. <!--- Update Function --->
  31. <cffunction name="updateByID" returntype="any" access="remote" returnFormat="json">
  32.     <cfargument name ="id" type="any">
  33.     <cfargument name ="modification" type="any">
  34.     <cfset query = { "_id" = {"$oid" = "#id#"}} >
  35.     <cfset query = parseJSON(query) >
  36.     <cfset modificationFields = {"$set" = #modification#}>
  37.     <cfset doc = SerializeJSON(modificationFields)>
  38.     <cfset doc = parseJSON(doc)>
  39.     <cfset document.findAndModify(query, doc)>
  40.     <cfreturn "done">
  41. </cffunction>
  42. <!--- Read Functions --->
  43. <cffunction name="getById" returntype="any" access="remote" returnFormat="json">
  44.     <cfargument name ="id" type="any">
  45.     <cfset response = document.findOne(ObjectID.init(id))>
  46.     <cfreturn response>
  47. </cffunction>
  48. <!--- Destroy Function --->
  49. <cffunction name="delete" returntype="void" access="remote" returnFormat="json">
  50.     <cfargument name ="id" type="any">
  51.     <cfset query = { "_id" = {"$oid" = "#id#"}} >
  52.     <cfset document.findAndRemove(parseJSON(query))>
  53. </cffunction>
  54. <!--- Using CRUD Operations --->
  55. <cfset example = {
  56.     "name" = "Foo",
  57.     "age" = 26,
  58.     "cancel" = true,
  59.     "createdAt" = now(),
  60.     "nested" = {
  61.         "value1" = 5,
  62.         "value2" = "abcdef...",
  63.         "value3" = true
  64.     }
  65. }>
  66. <!--- <cfset create(example)> --->
  67. <cfset values = {
  68.     "cancel" = false
  69. }>
  70. <cfset updateById(id="5720e5eda8d1e019284fb315", modification=values)>
  71. <!---<cfset  select = getById("571e5d84a8d1e0249066aac5")>
  72. <cfoutput>
  73.     #select['age']#
  74. </cfoutput>--->
  75. <cfset delete("571e5d84a8d1e0249066aac5")>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement