I want to write a MEAN stack program that reads and adds data into a mongodb database. My program works now without the mongodb part(works with array instead of the db). can you help me connect it to the mongo. I'm absolutely lost my way while searching about it. Thanks.
-
2...Not sure how to help when you don't show any code and don't explain what you have tried. – Matthew Bakaitis Jan 12 '15 at 14:39
-
I have no code because i found nothing about it. I found a big program that use mean stack but it so complicated and i gave up. i am looking for simple example for how the connection between mongo and the program should be. – user3100708 Jan 12 '15 at 14:40
3 Answers
You'll want to create the connection on the node server side. Here is my current setup on for a stack.
This is my server.js file
// modules =================================================
var express = require('express'),
mongoose = require('mongoose');
// Node Environment Configuration ===========================================
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
config = require('./server/config/config')[env];
// Create an Instance of Express ===========================================
var app = express();
// Modules of app ===========================================
require('./server/config/express.js')(app, config); // Express Configuration
require('./server/config/mongoose.js')(config); // Database Configuration
require('./server/config/routes.js')(app); // Routes Configuration
require('./server/config/passport.js')(); // Passsport Configuration
// Databse Connection ==================================================
mongoose.connect(config.db);
var db = mongoose.connection;
// start app ===============================================
app.listen(config.port);
console.log('listening on port ' + config.port); // shoutout to the user
exports = module.exports = app; // expose app
Mongoose config:
module.exports = function(config) {
mongoose.connect(config.db);
var db = mongoose.connection;
db.on('error', function callback () {
console.log("Connection error");
});
db.once('open', function callback () {
console.log("Mongo working!");
});
}
Here is my Database configuration
var path = require('path');
var rootPath = path.normalize(__dirname + '/../../');
module.exports = {
development:{
db: 'mongodb://localhost:27017/mean-demo',
rootPath: rootPath,
port: process.env.PORT || 3000
},
production:{
db: 'mongodb://mypath:pathname@ds041160.mongolab.com:47350/pathname',
rootPath: rootPath,
port: process.env.PORT || 80
}
}
Here's my express module:
var express = require('express'),
logger = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
passport = require('passport');
module.exports = function(app, config){
app.set('view engine', 'ejs');
app.set('views', 'server/views');
app.use(cookieParser()); //required for auth sessions
app.use(bodyParser()); //must come after cookie parser
app.use(session({secret: 'healing center'})); //required for auth sessions
app.use(passport.initialize()); //initialize passport middleware
app.use(passport.session()); //telling passport to use sessions
app.use('/js', express.static(config.rootPath + '/client/js'));
app.use(methodOverride('X-HTTP-Method-Override')); // simulate delete/put
app.use(express.static(config.rootPath + '/client')); // set the static files location /client/img will be /img for users
}
This setup is searching for the node environment you are currently in. Production or Development, Development hits the connection on the localhost. Production goes to mongolab where I've set up a database.

- 5,197
- 12
- 48
- 78
You should take a look at mongoose:

- 812
- 11
- 13
-
I know how to use mongoDB and i have already created database and managed him pretty good. My problem is to connect it with all the other controllers and js files – user3100708 Jan 12 '15 at 14:38
Here is a great tutorial from thinkster for getting started with the MEAN stack: https://thinkster.io/angulartutorial/mean-stack-tutorial/
Since it sounds like you have the AngularJS side in hand, you'll mostly be interested in the second half. It uses Mongoose which you may or may not want to use, but it should be a good example of how things fit together.
The MongoDB docs are also useful since you're doing everything in Javascript.

- 2,894
- 2
- 15
- 24