Mongoose populate query and unwrap the objects to obtain an array of strings

Viewed 58

I have the following query with mongoose:

const user = await this.userModule.findById(id).populate('roles', 'label -_id');

The output will look like this:

{
  _id: new ObjectId("61e2bc757644758d31578c7d"),
  email: 'hehehe@hehe.com',
  roles: [ { label: 'basic' }, { label: 'admin' } ]
}

My question is if there is a way to achieve something like this:

{
  _id: new ObjectId("61e2bc757644758d31578c7d"),
  email: 'hehehe@hehe.com',
  roles: [ 'basic', 'admin']
}

I want to get the value of each label property and add it to the roles array instead of adding an object like {label: basic}

0 Answers
Related