node.js - MongoDB ObjectId usage -
i'm using mongodb mongoose odm nodejs rest project:
my model schema is:
var playerschema = new mongoose.schema({ name: string, team: mongoose.schema.types.objectid })
and server side:
app.post('/players', function(req, res) { players.find(function(err, players) { res.json(players); }); });
the response is:
... { "_id": "511a6010e6ca7b0fe0af02ff", "name": "player-1", "team": "511a53e2e6ca7b151c09ce8d" } ...
but want like:
{ "_id": "511a6010e6ca7b0fe0af02ff", "name": "player-1", "team": { _id: "511a53e2e6ca7b151c09ce8d" name: "team-1" } }
what i'm doing wrong? or haven't understood objectid?
thanks!
you fetching players document have id of team document.
so each player have team doc also.
players.find(function(err, players) { for(var in players){ team.findbyid(players[i].team,function(error,teams){ players[i].team = teams; }) } res.json(players); });
node.js mongodb mongoose objectid
No comments:
Post a Comment