How to rename path in response for populate How to rename path in response for populate mongoose mongoose

How to rename path in response for populate


You can do this by virtual populate, introduced in mongoose version 4.5 . For that you need to define a virtual field in mongoose schema.

var GallerySchema = new mongoose.Schema({    name: String,    objectId: {        type: mongoose.Schema.Types.ObjectId    },});GallerySchema.virtual('user', {    ref: 'User',    localField: 'objectId',     foreignField: '_id' });

Ans when you run find query, just populate it with user.

Gallry.find({_id: galleryId}).populate('user','firstName lastName').exec(function(error, gallery) {    console.log(error);    console.log(gallery);;});

Above code is not tested in program, there may be typos, You can get more details about mongoose virtual populate on below link

http://mongoosejs.com/docs/populate.html