Carlos B.
Carlos B.

Reputation: 107

Populate array with object ID inside

I have this in my model:

User.js:

...
  usergroups: [{usergroup:{type: mongoose.Schema.Types.ObjectId, ref: 'UserGroup'},
                status: {type: String, default: 'Pending'}}],
...

When a user joins a user group, the ID of the group gets stored in the usergroup field of the user model array, and the status field defaults to Pending, awaiting approval from an admin. I can populate an array of IDs no problem, but I am not sure how to do it when the field to populate is paired with another one.

Am I getting my model wrong or missing something obvious?

Help please!

Thank you

Upvotes: 0

Views: 1002

Answers (1)

codephobia
codephobia

Reputation: 1590

Is this what you are looking for?

Schema.find({}).populate('usergroups.usergroup').exec(...);

Upvotes: 2

Related Questions