How to use MongoDB with ColdFusion

Query MongoDB Data in ColdFusion

Today, I would like to discuss on writing standard ColdFusion data access code to connect to MongoDB.

The CData JDBC Driver for MongoDB seamlessly integrates connectivity to MongoDB data with the rapid development tools in ColdFusion. This article shows how to connect to MongoDB data in ColdFusion and query MongoDB tables.

1. Create a JDBC Data Source for MongoDB in ColdFusion

The JDBC data source enables you to execute SQL from standard ColdFusion tags like cfquery and CFScript like executeQuery.

  • Copy the driver JAR and .lic file from the installation directory onto the ColdFusion classpath. For example, copy the files into C:\MyColdFusionDirectory\cfusion\wwwroot\WEB-INF\lib. Or, open the Java and JVM page in the ColdFusion Administrator and enter the path to the files in the ColdFusion Class Path box.
  • The JAR and license for the driver are located in the lib subfolder of the installation directory.
  • Restart the server after this step.

2. Add the driver as a data source:

From the ColdFusion administrator interface, expand the Data & Services node and click Data Sources. In the Add New Data Source section, enter a name for the data source and select Other in the Driver menu.

Data Source

3. Populate the driver properties:

    • JDBC URL: Enter connection properties in the JDBC URL. The JDBC URL begins with jdbc:mongodb: and is followed by the connection properties in a semicolon-separated list of name=value pairs. A typical JDBC URL is below:
    • View source
    • Jdbc:mongodb:Server=MyServer;Port=27017;Database=test;User=test;Password=Password;
    • Set the Server, Database, User, and Password connection properties to connect to MongoDB. To access MongoDB collections as tables you can use automatic schema discovery or write your own schema definitions. Schemas are defined in .rsd files, which have a simple format. You can also execute free-form queries that are not tied to the schema.
    • Driver Class: Enter the driver class. The driver class is cdata.jdbc.mongodb.MongoDBDriver.
    • Driver Name: Enter a user-defined name for the driver.
    • Username: Enter the username used to authenticate.
  • Password: Enter the password used to authenticate.

Populate the Driver Properties

You can now test the connection by enabling the CData MongoDB data source in the Actions column. After reporting a status of OK, the MongoDB data source is ready for use.

CData MongoDB Data Source

Execute Queries

The cfquery tag can pass SQL statements to MongoDB, including INSERT, UPDATE, and DELETE.. Use the cfqueryparam tag to create parameterized queries and prevent SQL injection through the query string.

SELECT * FROM restaurants WHERE Name =

Below is the equivalent in CFScript:

<cfscript>

result = queryExecute(
“SELECT * FROM restaurants WHERE Name = ?”,
[
{ value=”Morris Park Bake Shop”, cfsqltype=”cf_sql_varchar” }
],
{ datasource=”CDataMongoDB” }
);
writeDump( var= result );

</cfscript>

You can then make requests to your .cfm like the following:

http://MyServer:8500/query.cfm?Name=Morris Park Bake Shop

Execute Queries