I have a collection of list that contain invitee information as an array of objects like below:
const InviteeListSchema = new Schema([
{
//add this part later on to link a list to the host
// it should look up for host ID etc. Make sure it is INT/NUMBER
// host_id: {},
ownerId: { type: Schema.Types.ObjectId, ref: "Customer_Host", required: true },
invitees: [
{
_id: { type: Schema.Types.ObjectId, required: true, auto: true },
firstname: { type: String, required: true },
lastname: { type: String, required: false },
email: { type: String, required: true },
//emailConfirm: { type: String, required: true },
mobile: { type: String, required: false },
createdAt: { type: Date, required: true, default: Date.now() },
lastUpdatedAt: { type: Date, required: true, default: Date.now() },
isDeleted: { type: Boolean, required: true, default: false }
}
],
createdAt: { type: Date, required: true, default: Date.now() },
lastUpdatedAt: { type: Date, required: true, default: Date.now() },
isDeleted: { type: Boolean, required: true, default: false }
}]);
I want to be able to add/push new objects into the invitees array. However, I am having issues getting the newly created object within invitees array.
I have tried findandupdate, but returns the entire object. tried updateOne, but doesn’t return the new objectID. I am wondering if there is a one step/elegant way to achieve this? Otherwise it requites at least 2 steps, where the second step would be a find() method to fetch the last array item from invitees.
Keen to hear the experts thoughts on this.
Thanks in advance