Created
January 19, 2012 15:10
-
-
Save ovaillancourt/1640519 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var dbUrl = 'DB_URL HERE'; | |
//Initialize mongoose | |
var mongoose = require('mongoose'); | |
mongoose.connect( | |
dbUrl, | |
{auto_reconnect: true, poolsize: 5} | |
); | |
//Player SChema | |
var PlayerSchema = new Schema({ | |
firstname: {type: String}, | |
lastname: {type:String}, | |
//Other info here | |
}); | |
var Player = mongoose.model('Player',PlayerSchema); | |
//Lightweight version of a player that we use to pre-fill a team roster | |
//in order to perform most team-related queries without having to load | |
//a complete player profile everytime. | |
var CachedPlayer = new Schema({ | |
playerRef: {type: Schema.ObjectId, ref: 'Player'}, | |
//And we only cache parts of the info | |
lastname: {type: String} | |
}); | |
//Team Schema | |
var TeamSchema = new Schema({ | |
teamname: {type:String}, | |
roster :[CachedPlayer], | |
//Other info here | |
}); | |
var Team = mongoose.model('Team',TeamSchema); | |
//Let's create some dummy players | |
var Iginla = new Player(); | |
Iginla.firstname = 'Jarome'; | |
Iginla.lastname = 'Iginla'; | |
Iginla.save(function(){ | |
var Crosby = new Player(); | |
Crosby.firstname = 'Sidney'; | |
Crosby.lastname = 'Crosby'; | |
Crosby.save(function(){ | |
var Malkin = new Player(); | |
Malkin.firstname = 'Evgeni'; | |
Malkin.lastname='Malking'; | |
Malkin.save(function(){ | |
//Let's put them into team canada and the pittsburg's penguin | |
var Penguin = new Team(); | |
Penguin.roster.push({_id: Crosby._id, lastname: Crosby.lastname}); | |
Penguin.roster.push({_id: Malkin._id, lastname: Malkin.lastname}); | |
Penguin.save(function(){ | |
var Canada = new Team(); | |
Canada.roster.push({_id: Crosby._id, lastname: Crosby.lastname}); | |
Canada.roster.push({_id: Iginla._id, lastname: Iginla.lastname}); | |
Canada.save(function(){ | |
//Now I'd like all the teams that have Jarome Iginla AND Sidney crosby in their | |
//roster. (I received the Ids from a user search for example.) | |
Team.find({'roster.playerRef' : {$all: [Crosby._id, Iginla._id]}},function(e,d){ | |
console.log('err: ', e); //<-- Here's the error. | |
console.log('doc: ', d); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment