mongodb - How to add a sub document model in a collection collection or populate it -


i tried ask question, guess wasn't clear enought since nobody answered, going give specific scenario code here.

the context simple, let's imagine weekly tournament, each tournament composed games, composed players.

so have 3 schemas:

tournamentschema {     games: [ { type: mongoose.schema.types.objectid, ref: 'game' } ]     players: [          {              _user: { type: mongoose.schema.types.objectid, ref: 'user' },             points: { type: number }         }      ] //the total amount of points user made tournament  }  gameschema {     players: [          {              _user: { type: mongoose.schema.types.objectid, ref: 'user' },             points: { type: number }         }      ] //the total amount of points user made specific game }  playerschema {     pseudo: { type: string },     email: { type: string },     points: { type: number } // total amount player made                               // (on every tournaments). } 

everything fine utill here. not make find() et sort() points request every time client want display rank (the tournament.players collection sorted points), keep tournament instance specific module (so can slice() part of sorted players collection need treat).

so everytime player joins game, have :

  1. create new game instance , add him , save it
  2. add him existing tournament.players collection , save it.

the problem is, can't push model tournamanent.players collection, guess mongoose can't manage (the player model turns objectid once inserted):

var newplayer = new playerschema(     { pseudo: 'warior', email: 'warioir@something.com' } );  tournament.players.push({ _user: newplayer, points: 0 })  //add item: { _user: <id>, _id: <id>, points: 0 } pseudo?? email?? 

so when push full model player populated tournament collection of players, becomes objectid.

my issue right here, since after have tournament.players collection populated , un-populated players, it's pain manage ! every time push player model, make new find() populate class on whole tournament , replace old instance 1 get, that's big operation specific need.

how manage situation (need add model on populated collection stored in module) ?

i'd in 2 steps. when new player joins, create entry in user table, _id, insert game , tournament collections. mongoose isn't "magical" other orms may have used in past. did 1k line change remove mongoose project , use native drivers instead.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -