Skip to content

Instantly share code, notes, and snippets.

@nagyv
Created April 4, 2012 13:37
Show Gist options
  • Save nagyv/2301095 to your computer and use it in GitHub Desktop.
Save nagyv/2301095 to your computer and use it in GitHub Desktop.
The simplest Mongoose app to run
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var myS = new Schema({
name: {type: String, required: true}
});
var My = mongoose.model('My', myS);
var my = new My({'name': 'bika'});
my.save(function(err){
console.log(err, 'saved');
db.close();
process.exit();
});
/** Using only this would never give you a save */
//var db = mongoose.createConnection('mongodb://127.0.0.1:27017/test');
/** You need these */
mongoose.connect('mongodb://127.0.0.1:27017/test');
var db = mongoose.connection
db.on('open', function(err){
console.log('db open');
if(err) throw err;
});
db.on('error', function (err) {
console.log(err);
});
// Just to avoid premature end of script
var express = require("express"),
app = express.createServer();
app.listen(3000);
@nagyv
Copy link
Author

nagyv commented Apr 4, 2012

For some reason the above code does not save anything to the db, no callback is ever called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment