Created
December 11, 2016 21:02
-
-
Save bitgord/014583993f84f2f6c2b4fbc40d57a515 to your computer and use it in GitHub Desktop.
Quick setup of a MongoLab database
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
// Node and npm are required for this gist | |
// Install mongoose | |
npm install mongoose | |
// On https://mlab.com/create click on Free Sandbox and give your DB a name | |
// You are then given a database on the next screen | |
// Give your database a new username and password | |
// The mongo shell and MongoDB URI should now be visible | |
// In your project folder, make a config folder and a config.json file with your uname and password | |
{ | |
"uname": "[[insert uname]]", | |
"pword": "[[insert pword]]" | |
} | |
// Make a new file in the config folder called index.js and require config | |
var configValues = require('./config'); | |
// Export an object and connect to mongo | |
module.exports = { | |
getDbConnectionString: function () { | |
return 'mongodb://' + configValues.uname ':' + configValues.pwd + '@ds019058.mlab.com/[[insert database name]]' | |
} | |
} | |
// Create a new folder called models and a new file called mongoModel.js | |
// Require mongoose | |
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var mongoSchema = new Schema({ | |
item: String, | |
item2: Boolean | |
}); | |
var Mongo = mongoose.model('Mongo', mongoSchema); | |
// Export the model | |
module.exports = Mongo; | |
// In app.js require mongoose, config folder and connect to mongoose | |
mongoose.connect(config.getDbConnectionString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment