1

I have two schemas - Match and Member. Match contains an array of Members.

var MatchSchema = new Schema({name: String, players: [{ player: { type: Schema.ObjectId, ref: 'Member'}, play: Boolean}]}); var MemberSchema = new Schema({ firstName: String, lastName: String}); 

I'm trying to populate the the ObjectIds with per http://mongoosejs.com/docs/populate.html

.populate({ path: 'players', populate: {path: 'player'} }) 

Per Mongoose debug, the matches.find query returns the two expected Member ObjectIds, ...

Mongoose: matches.findOne({ _id: ObjectId("5883a57362f0ca10945b305a") }, { fields: undefined }) Mongoose: matches.find({ _id: { '$in': [ ObjectId("58502d28e88746467d48b424"), ObjectId("583c82ef5274831a5443fa53")] } }, { fields: undefined }) 

but match.players.player is not populated with firstName or lastName.

I believe that I've tried all of the combinations/options of population syntax. Any ideas?

2
  • What did you console to get the result you have put in the question. I have not seen Mongoose giving result like this. Commented Jan 22, 2017 at 5:49
  • Simply add this code to your server -mongoose.set('debug', true); May sure you are looking at the server console/terminal and not the client. Commented Feb 23, 2017 at 17:00

1 Answer 1

1

You are going in the right path but doing one mistake in populate.

You should populate players.player instead of nested population( populating players and player inside that). players is not a reference, but an array, and players.player is a reference, and that's why your method isn't working.

Try this:

.Populate('players.player') 

This should work for you.

Sign up to request clarification or add additional context in comments.

5 Comments

That didn't work for me. I'm in the process of restructuring my data to make server calls less complex. Will advise if that works for me.
what is the error it is showing?, and what are the structural changes that you are doing?
The same error as shown above {fields: undefined}. I've eliminated the Object in the players array and just using an array of Member ObjectIds.
This doesn't work with a large quantity of populate() call
what do you mean by large quantities of populate() call? Can you show me your schema? or maybe you can create a new question and tell me the link here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.