______________________________________________________________________________
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
- To use mongodb on your machine, firstly you have to download it from this link and install it
- Install CF2021 and then install the cfmongodb module using the following steps –
- Open command prompt and navigate to <ColdFusion installation directory>/cfusion/wwwroot/bin
- Run cfpm.bat
- Enter the command install cfmongodb
Create your first MongoDB application
Method 1
- Create a folder testMongo in <ColdFusion installation directory>/cfusion/wwwroot/
- Create a file Application.cfc in the testMongo folder with the code below. Here, we are creating a datasource mongoLocal.
component { |
Method 2
You can also create a NOSQL datasource directly from CF Admin instead of using Application.cfc. To do so, follow below steps:
- Navigate to http://localhost:8500/CFIDE/administrator/index.cfm.
- Navigate to Data & Services >> NOSQL Datasources.
- Enter a Datasource name as mongoLocal & select type as MongoDB.
- Enter host as localhost & port as 27017.
- 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([{ writedump(var=res , label ="Insertion result"); |
READ
To read the documents present in the collection, the code snippet is as follows –
collection.find().foreach((c, i) => { |
UPDATE
To update any of the documents you can use the updateOne method to update it as shown –
res = collection.update( |
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"}) |
For detailed documentation about all the features that are available you can refer to this link –
ColdFusion MongoDB Documentation
You must be logged in to post a comment.