Skip to content
Randy Merrill edited this page Jan 5, 2012 · 1 revision

Mongo4CF is designed to be used as an application singleton. When you create the Mongo object you establish the connection to the MongoDB server and the Java driver controls the connection pool. When your application is finished, close the connections to the MongoDB server.

Usage

Here is an example of creating a singleton in Application.cfc:

component {
	public boolean function onApplicationStart() {
		// Connect to the MongoDB server
		application.mongo = createObject('component', 'mongo4cf.mongo').init();
	}
	
	public void function onApplicationEnd(required struct applicationScope) {
		// Close the MongoDB connections
		arguments.applicationScope.mongo.close();
	}
}

Once you have your mongo singleton you can connect to a database, select a collection, insert a new record, and find the record again:

db = application.mongo.getDB('mongo4cf_test');
collection = db.getCollection('testCollection');

collection.save({
	'name': 'Fred',
	'email': '[email protected]'
});

writeDump(collection.findOne());

Additional Information

Clone this wiki locally