Skip to content

Instantly share code, notes, and snippets.

@coderoshi
Created February 21, 2012 01:52
Show Gist options
  • Save coderoshi/1872894 to your computer and use it in GitHub Desktop.
Save coderoshi/1872894 to your computer and use it in GitHub Desktop.
MongoHQ NodeJS Connection
// npm install mongodb
var mongodb = require('mongodb');
var url = require('url');
var log = console.log;
var connectionUri = url.parse(process.env.MONGOHQ_URL);
var dbName = connectionUri.pathname.replace(/^\//, '');
mongodb.Db.connect(process.env.MONGOHQ_URL, function(error, client) {
if (error) throw error;
client.collectionNames(function(error, names){
if(error) throw error;
// output all collection names
log("Collections");
log("===========");
var lastCollection = null;
names.forEach(function(colData){
var colName = colData.name.replace(dbName + ".", '')
log(colName);
lastCollection = colName;
});
var collection = new mongodb.Collection(client, lastCollection);
log("\nDocuments in " + lastCollection);
var documents = collection.find({}, {limit:5});
// output a count of all documents found
documents.count(function(error, count){
log(" " + count + " documents(s) found");
log("====================");
// output the first 5 documents
documents.toArray(function(error, docs) {
if(error) throw error;
docs.forEach(function(doc){
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