I have a Message
schema:
const messageSchema = new Schema({
receiver:{
type: mongoose.Schema.Types.ObjectId,
ref:'User'
},
sender:{
type:mongoose.Schema.Types.ObjectId,
ref:'User'
},
room:{
type:String
},
message:{
type:String
}
},{timestamps:true});
As you can see I am holding a reference to sender
.I am getting all messages using:
const messages = await Message.find({room}).sort({createdAt:1}).populate('sender',{email:1,_id:0});
and this returns:
[
{
_id: 60b2725c3165d72d1a627826,
receiver: 60abb9e1016b214c7563c8f1,
sender: { email: 'test2@test.com' },
room: 'test@test.com--with--test2@test.com',
message: 'dfgfdsgdf',
createdAt: 2021-05-29T16:57:00.857Z,
updatedAt: 2021-05-29T16:57:00.857Z,
__v: 0
}
]
I want to remove the email
key from the response. So the sender field should be like sender:'test2@test.com'
.Is there any way to do this?