Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created September 17, 2013 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/6589063 to your computer and use it in GitHub Desktop.
Save bennadel/6589063 to your computer and use it in GitHub Desktop.
Getting Started With MongoLab And The MongoDB Shell
// Clear any existing data from the friends collection.
db.friends.remove();
print( "> Friends collection has been reset." );
// Set up the randomly genreated friends.
var names = [
"Tricia", "Joanna", "Kim", "Anne", "Libby",
"Sarah", "Kate", "Kristina", "Samantha", "Tina"
];
// Add each name to the collection.
names.forEach(
function( name ) {
var friend = {
name: name,
age: ( 18 + Math.floor( Math.random() * 50 ) )
};
db.friends.insert( friend );
}
);
print( "> Friends collection has been populated with test data." );
// Because we are going to be using a remote connection, be sure
// to start the MongoDB Shell (mongo) with the --nodb flag. Then,
// we can connect and define our own db instance.
// Connect to the MongoLab database.
var connection = new Mongo( "ds045628.mongolab.com:45628" );
// Connect to the test database.
var db = connection.getDB( "bennadel-test" );
// Authorize this connection.
db.auth( "**************", "**************" );
print( "> MongoLab connection and DB defined." );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment