Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created June 19, 2012 17:29
Show Gist options
  • Save aheckmann/2955422 to your computer and use it in GitHub Desktop.
Save aheckmann/2955422 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'testing_11074799');
mongoose.connection.on('error', function () {
console.error(arguments);
});
var UserSchema = new Schema({ name: String });
var User = mongoose.model('User', UserSchema);
var PlayerSchema = new Schema({
user: {type: Schema.ObjectId, ref : 'User'}
, score : {type : Number, default : 0 }
});
var GameSchema = new Schema({
players : {type : [PlayerSchema], required : true}
});
var Game = mongoose.model('Game', GameSchema);
mongoose.connection.on('open', function () {
User.create({ name: 'aaron' }, { name: 'bob' }, function (err, aaron, bob) {
if (err) return console.error(err.stack||err);
console.error('aaron', aaron);
console.error('bob', bob);
var game = new Game;
game.players.push({ _id: aaron._id, user: aaron._id, score: 4 });
game.players.push({ _id: bob._id, user: bob._id, score: 3 });
console.error('ready to save game', game.players);
mongoose.set('debug', true);
game.save(function (err) {
if (err) return console.error(err.stack||err);
console.error('saved', game);
Game.findOne().populate('players.user').exec(function (err, game) {
if (err) return console.error(err.stack||err);
console.error('aaron scored %d points', game.players.id(aaron.id).score);
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