Created
February 21, 2012 02:02
-
-
Save coderoshi/1872995 to your computer and use it in GitHub Desktop.
MongoHQ NodeJS Connection (Coffee)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# npm install mongodb | |
mongodb = require 'mongodb' | |
url = require 'url' | |
log = console.log | |
connection_uri = url.parse(process.env.MONGOHQ_URL) | |
db_name = connection_uri.pathname.replace(/^\//, '') | |
mongodb.Db.connect process.env.MONGOHQ_URL, (error, client)-> | |
throw error if error | |
client.collectionNames (error, names)-> | |
throw error if error | |
# output all collection names | |
log "Collections" | |
log "===========" | |
last_collection = null | |
for col_data in names | |
col_name = col_data.name.replace("#{db_name}.", '') | |
log col_name | |
last_collection = col_name | |
collection = new mongodb.Collection(client, last_collection) | |
log "\nDocuments in #{last_collection}" | |
documents = collection.find({}, limit : 5) | |
# output a count of all documents found | |
documents.count (error, count)-> | |
log " #{count} documents(s) found" | |
log "====================" | |
# output the first 5 documents | |
documents.toArray (error, docs)-> | |
throw error if error | |
for doc in docs then log doc | |
# close the connection | |
client.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment