Looking for Mongoose "hello world" example
For Mongoose 2.7 (tested in Node v. 0.8):
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var db = mongoose.connect('mongodb://localhost/db');
var User = new Schema({
first_name: String,
last_name: String
});
var UserModel = mongoose.model('User', User);
var record = new UserModel();
record.first_name = 'hello';
record.last_name = 'world';
record.save(function (err) {
UserModel.find({}, function(err, users) {
for (var i=0, counter=users.length; i < counter; i++) {
var user = users[i];
console.log( "User => _id: " + user._id + ", first_name: " + user.first_name + ", last_name: " + user.last_name );
}
});
});
You need to define the schema. Try this: (
var mongoose = require('mongoose').Mongoose,
db = mongoose.connect('mongodb://localhost/db'),
Schema = mongoose.Schema;
mongoose.model('User', new Schema({
properties: [
'name',
'age'
],
[...]
}));