December 7, 2020
MongoDB Integration with CF2021
Comments
(0)
December 7, 2020
MongoDB Integration with CF2021
Newbie 1 posts
Followers: 0 people
(0)

______________________________________________________________________________
This is a blog about the new feature in ColdFusion. You can now integrate it with MongoDB, a NoSQL database.


Introduction

NoSQL databases (aka “not only SQL”) are non tabular, and store data differently than relational tables. NoSQL databases come in a variety of types based on their data model. The main types are document, key-value, wide-column, and graph. They provide flexible schemas and scale easily with large amounts of data and high user loads.

MongoDB is a cross-platform document-based NoSQL database. It uses JSON/BSON like documents with schema. It stores data in flexible, JSON-like documents. The document model maps to the objects in your application code, making data easy to work with. It is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use.

INSTALLATION

  1. To use mongodb on your machine, firstly you have to download it from this link and install it
  2. Install CF2021 and then install the cfmongodb module using the following steps –
    1. Open command prompt and navigate to <ColdFusion installation directory>/cfusion/wwwroot/bin
    2. Run cfpm.bat
    3. Enter the command install cfmongodb

Create your first MongoDB application

Method 1 

  1. Create a folder testMongo in <ColdFusion installation directory>/cfusion/wwwroot/
  2. Create a file Application.cfc in the testMongo folder with the code below. Here, we are creating a datasource mongoLocal.
component {
      this.name = "mongo";
            this.datasources = {
           
            "mongoLocal"= {
                  type="mongodb",
                  host="mongodb://localhost:27017/",
                  "init": true                  
            }
      }
}

Method 2

You can also create a NOSQL datasource directly from CF Admin instead of using Application.cfc. To do so, follow below steps:

  1. Navigate to http://localhost:8500/CFIDE/administrator/index.cfm.
  2. Navigate to Data & Services >> NOSQL Datasources.
  3. Enter a Datasource name as mongoLocal & select type as MongoDB.
  4. Enter host as localhost & port as 27017.
  5. Click Add

Then you can see your datasource added to the same screen. The datasource can be verified as well there itself.

CRUD Operations

You can perform all the Create Read Update and Delete i.e. CRUD operations on your MongoDB using small snippets of code as shown below. (Am showing them using the script syntax here)

Firstly Get select the database using the dataSource created 

db = GetMongoService("mongoLocal").db("mydb")  

Next select the collection within the database

collection = db.Students

INSERT

Now to perform insertion of documents into this collection the code snippet is as follows. It is an example of inserting two students along with their details into the students database

res = collection.insertMany([{
      enrollno: "110470116021",
  name: "David Thompson",
      college: "NYU",
      course: {
          courseName: "Bachelors IT",
      duration: "4 Years"
     },
  address: {
      city: "New York",
      state: "New York",
       country: "USA"
    }
},{
enrollno: "110470116022",
  name: "Chris Raymond",
      college: "UCLA",
      course: {
          courseName: "Diploma IT",
      duration: "4 Years"
  },
  address: {
      city: "Los Angeles",
      state: "CA",
      country: "US"
    }
    }
}])
writedump(var=res , label ="Insertion result");
writeoutput("number of documents in the collection: <b>" & collection.count() & " </b><br/><br/>")
writeoutput("<br/><b> find all documents inserted </b><br/>")

READ 

To read the documents present in the collection, the code snippet is as follows – 

 collection.find().foreach((c, i) => {
writedump(var= c, label = "document " & i );
writeoutput("<br/>")
})

UPDATE

To update any of the documents you can use the updateOne method to update it as shown –

res = collection.update(
  { name: "Chris Raymond" },
  {
    $set: {college: "stony brook"}
  }
)

writedump(var=res , label ="Update result");
writeoutput("<br/><b> find document by filter and get first </b><br/>")
res = collection.find({college: "stony brook"}).first()
writedump(res);

DELETE

Finally for deletion of documents, you can use the deleteOne or deleteMany functions to delete them. Here the usage of deleteOne is shown – 

res = collection.deleteOne({name: "Chris Raymond"})

writedump(var=res , label ="Deletion result");
writeoutput("number of documents in the collection: <b>" & collection.count() & " </b><br/>")

For detailed documentation about all the features that are available you can refer to this link –
ColdFusion MongoDB Documentation

0 Comments
Add Comment