lean() inside populate in mongoose
Here it is.
Pins.find(condition) .limit(limit) .sort({time: -1}) .populate({ path: 'user_id', select: '_id name pic twitter_id fb_id', options: { lean: true} }) .lean().exec(function(err, pins) { if(pins){ pins = _.map(pins,function(pin) { //Setting the flag for social account pin.socialaccount = (pin.user_id.twitter_id || pin.user_id.fb_id ); //Omitting the twitter_id and fb_id fields from user_id object pin.user_id.twitter_id = undefined; pin.user_id.fb_id = undefined; return pin; }); return res.send(pins); }})
I have mapped the pins collection from the find query such that you can set your socialaccount flag and can set the properties not required to undefined
However i am not sure if you can use _.omit(pin.user_id,'twitter_id')
instead of pin.user_id.twitter_id = undefined
.Need to test that !
The result pins collection will now have other user properties you wanted.Hope it helps :)