óCoffeeScript Cookbook

MongoDB

Problem

You need to interface with a MongoDB database.

Solution

For Node.js

Setup

Saving Records

mongo = require 'mongodb'

server = new mongo.Server "127.0.0.1", 27017, {}

client = new mongo.Db 'test', server, {w:1}

# save() updates existing records or inserts new ones as needed
exampleSave = (dbErr, collection) ->
	console.log "Unable to access database: #{dbErr}" if dbErr
	collection.save { _id: "my_favorite_latte", flavor: "honeysuckle" }, (err, docs) ->
		console.log "Unable to save record: #{err}" if err
		client.close()

client.open (err, database) ->
	client.collection 'coffeescript_example', exampleSave

Finding Records

mongo = require 'mongodb'

server = new mongo.Server "127.0.0.1", 27017, {}

client = new mongo.Db 'test', server, {w:1}

exampleFind = (dbErr, collection) ->
	console.log "Unable to access database: #{dbErr}" if dbErr
	collection.find({ _id: "my_favorite_latte" }).nextObject (err, result) ->
		if err
			console.log "Unable to find record: #{err}"
		else
			console.log result # => {  id: "my_favorite_latte", flavor: "honeysuckle" }
		client.close()

client.open (err, database) ->
	client.collection 'coffeescript_example', exampleFind

For Browsers

A REST-based interface is in the works. This will provide AJAX-based access.

Discussion

This recipe breaks the save and find into separate examples in order to separate the MongoDB-specific concerns from the task of connection and callback management. The async module can help with that.