Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created July 30, 2012 17:13
Show Gist options
  • Save aheckmann/3208456 to your computer and use it in GitHub Desktop.
Save aheckmann/3208456 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
mongoose.connect('localhost', 'testing_embeddedVirtuals');
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var sessionSchema = new Schema({ from: { type: Date, required: true }});
sessionSchema.virtual('startTime').get(function () {
return 'start time was ' + this.from
});
var schema = new Schema({
name: String
, sessions: [sessionSchema]
});
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var a = new A({ name: 'embeddedVirtuals' });
a.sessions.push({ from: new Date });
console.log('a is ', a);
console.log();
a.save(function (err, a) {
if (err) return done(err);
A.findById(a, function (err, doc) {
console.log('retreived ', doc);
console.log();
console.log('startTime: ', doc.sessions[0].startTime);
done(err);
});
})
});
function done (err) {
if (err) console.error(err.stack);
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment