Skip to content

Instantly share code, notes, and snippets.

@OscarGodson
Created September 11, 2011 04:25
Show Gist options
  • Save OscarGodson/1209163 to your computer and use it in GitHub Desktop.
Save OscarGodson/1209163 to your computer and use it in GitHub Desktop.
How to connect to MongoHQ with Node + node-mongodb-native
/**
* To get all the info to login, sign into your MongoHQ account, go to the db you want,
* click the "Database Info" tab, then look for the line that looks like:
* -------------------------------------------------------------
* mongodb://<user>:<password>@staff.mongohq.com:10056/node-test
* ---------| |-| |------------------| |-| |
* USER PASSWORD PORT DB NAME
*
* ALSO, for testing, you should manually add a document and collection into MongoHQ
* from their "Add a Collection" > "Add a Document" links, then below we'll log it.
*/
//If you haven't already, install the mongodb package w/ npm install mongodb
var mongodb = require('mongodb');
var db;
//The 10056 is the port!
db = new mongodb.Db('example-db', new mongodb.Server('staff.mongohq.com', 10056, {auto_reconnect:true}), {});
db.open(function(err, p_client) {
//Notice the USERNAME and PASSWORD!
db.authenticate('USERNAME', 'PASSWORD', function(err) {
//Change error handler when going into production
if (err) console.log(err);
var collection = new mongodb.Collection(db, 'test_collection');
collection.find({}, {limit:10}).toArray(function(err, docs) {
//In an array, this will log all your documents you added before we tested this
console.dir(docs);
});
});
});
@OscarGodson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment