Is there a way to exclude certain attributes such as email, logoUri, and personal info from a seller object when dynamically populating?
Below is the messageSchema:
const messageSchema = mongoose.Schema({
chatId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "Chat",
},
....
// sender can be type of User or Seller
sender: {
type: mongoose.Schema.Types.ObjectId,
refPath: "senderType",
required: true,
},
senderType: {
type: String,
required: true,
enum: ["User", "Seller"],
},
readOn: { type: Date },
read: Boolean, //}, {
sentOn: {
type: Date,
default: Date.now,
},
});
Here is where I populate my schema:
const messages = await Message.find({ chatId: chatId })
.populate("sender")
.sort({
sentOn: -1,
});
Since each model has different attributes, is it possible to select based on the type of model being populated dynamically? For example, for the user model:
.select('-email -logoUri')
For the seller model:
.select('-address')