MongoDB with ColdFusion – {“some”: “notes”}

I’ve been looking recently at using MongoDB in work. I have a potential ‘big’ project in mind that might benefit from a document collection system like Mongo, where reads are the rule and updates are the exception.

Another use case is to use the concept of capped collections for logging – and that’s what I was setting up in my last post.

{“server”: “ColdFusion”, “database”: “Mongo”}

So I’d recommend using Marc Esher’s CFMongoDB project to get started using Mongo. The CFMongoDB implementation is based on the official Java drivers so feel free to build upon this excellent base.

Here are some notes on some issues I came across. Most were answered at the official CFMongoDB Google group, so if you have any questions – that is a good place to post questions etc.

{“topic”: “Configuration”}

The MongoConfig.cfc is the recommended place to extend the configuration. Here is an example one I used for my test environment:

<cfcomponent displayname="TestMongoConfig" extends="MongoConfig" output="false">

	<cffunction name="configureEnvironment" access="public" returntype="string" output="false"> 
		<cfset removeAllServers() />
		<cfset addServer("", "27017") />
		<cfreturn "test" />


Likewise a ProductionMongoConfig.cfc can hold your production Mongo details.

The whole concept of the Mongo _id field confused the crap out of me for a while until a few things were clarified for me:

1. If you create a document (e.g. a simple CF structure) and pass it to the Mongo instance using the save(doc, collection) method then a new key/value will be added to the document once Mongo saves it. This new key is called _id. Dump the document (i.e. the structure) afterwards and you’ll see a new object there with a ket of _id! There is lots of info in the official docs about what this key is and how it is generated.

2. When you call save(doc, collection) and this document is saved and you attempt to call save() on the same document again, then Mongo will actually use the _id to update the same document in the collection. This super important and means that the code below will only insert 100 documents rather than 200:

arr = [];
collection = "testCollection";

for (i=0; i < 10; i++) {
	doc = {ts=now(), name="value", data="blah_#i#"};
	arrayAppend(arr, doc);
// individual inserts
for (i=1; i <= arrayLen(arr); i++) {[i], collection);

// and the same documents again 
// but this time they will update because they  each have an _id
for (i=1; i <= arrayLen(arr); i++) {[i], collection);

{“topic”: “Authentication”}

Authentication is supported by MongoDB but is not directly supported by CFMongoDB yet. I’ve just worked that into a forked branch of Marc’s project and I hope to have it merged shortly with the main CFMongoDB branch.

If you don’t want to use authentication then make sure you secure those servers/TCP ports!

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

3 Responses to MongoDB with ColdFusion – {“some”: “notes”}

  1. Lester Kitson says:

    Hi Ciarán,

    There does not seem to be any tutorial on the internet which actually shows
    how to connect a Mongodb database to Coldfusion. So I am asking if you
    could you do a detailed tutorial showing step-by-step how to set up
    Coldfusion to work with or connect to Mongodb;

    1. By way of using the cfmongodb project and/or the Java Driver/JavaLoader
    2. Creating a simple CRUD with Coldfusion and Mongodb

    Much thanks in advance!

    • ciaranarcher says:

      I’m afraid I haven’t got the time to do this. I’d suggest following the standard MongoDB installation instructions and then. Once running cfmongodb should work ‘out of the box’.

  2. Pedro says:

    Thank your for sharing your experience with MongoDB and other things. I’m in the same situation that you, trying to create a system that uses Coldfusion and MongoDB and I’m already playing with CFMongoDB. Was satisfactory your experience with MongoDB and Coldfusion or you don’t recommend it?

    I saw in stackoverflow that you were using a singleton pattern to have only one instance of mongodb. My question here is, what do you actually have saved as singleton?:

    I mean, at the moment I have the mongo object stored in an application variable that it’s created during the application starts:
    application.mongo = createObject(‘component’,’inc.cfmongodb.core.MongoClient’).init(mongoConfig);

    Once I have this object saved there, do I have to use it directly from there?:…)
    or should I create a new variable and use this local variable instead of the application variable?:
    localmongo = application.mongo.getDBCollection(‘example’);…)

    And the last one, should I close this local variable (localmongo on my previous example) using localmongo.close()? Or is this not needed? Or should I close my application.mongo variable? Or shouldn’t I close any of them?
    I’m sorry, but I’m a bit lost and worried about if I have to close or not connections :S

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s