Created
September 17, 2013 01:38
-
-
Save bennadel/6589063 to your computer and use it in GitHub Desktop.
Getting Started With MongoLab And The MongoDB Shell
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
// 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." ); |
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
// 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