I cant for the life of me get mongoose working in my express app. Ive installed mongoose, and also mongodb via NPM (the mongoose documentation didn't state whether mongodb was required separately or how to get it up and running).
Here is the code im using.
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/my_database');
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var Comments = new Schema({
title : String
, body : String
, date : Date
});
var BlogPost = new Schema({
author : ObjectId
, title : String
, body : String
, date : Date
, comments : [Comments]
, meta : {
votes : Number
, favs : Number
}
});
var BlogPost = mongoose.model('BlogPost', BlogPost);
var post = new BlogPost();
post.title='blahblah';
// create a comment
post.comments.push({ title: 'My comment' });
post.save(function (err) {
if(err){
throw err;
console.log(err);
}else{
console.log('saved!');
}
});
Anyone have any idea what Im doing wrong? I dont understand whether I need to somehow start mongodb seperately or not (it looks like the mongoose.connect function starts the mongodb server right?)
But ya, nothing ever happens when I start my app (and it should be outputting either the error or saved! to my console when I save the test post right?
Anyways any help would be very much appreciated!
Thanks