How to fetch the _id of the subdocument from array

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

Are you passing the optional parameter to return the updated document as opposed to the document before the update?

This is what I get from each queries:

db.invitee_lists.updateOne({_id:ObjectId("64fa8e44cb08d093f33d0a08")},{$push: {'invitees':{'firstname':'Ilay'}}},{new:true, returnNewDocument: true})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}

and

db.invitee_lists.findOneAndUpdate({_id:ObjectId("64fa8e44cb08d093f33d0a08"),},{$push: {'invitees':{'firstname':'Ilay', "_id":new ObjectId()}}},{new:true, returnNewDocument:true})
_id: ObjectId("64fa8e44cb08d093f33d0a08"),
  ownerId: ObjectId("64f845f0f4e46cb7a0467053"),
  invitees: [
    {
      _id: ObjectId("64fa8e44cb08d093f33d0a09"),
      firstname: 'firstname',
      lastname: 'lastname',
      email: 'myemail@gmail.com',
      mobile: '0400000111',
      createdAt: 2023-09-08T01:40:51.946Z,
      lastUpdatedAt: 2023-09-08T01:40:51.946Z,
      isDeleted: false
    },
    {
      _id: ObjectId("64fa8e44cb08d093f33d0a0a"),
      firstname: 'Micky,
      lastname: 'Lee',
      email: 'myemail@yahoo.com',
      mobile: '+6140000000',
      createdAt: 2023-09-08T01:40:51.946Z,
      lastUpdatedAt: 2023-09-08T01:40:51.946Z,
      isDeleted: false
    },
   {
      firstname: 'Ilay',
      _id: ObjectId("65039d48b6a4eb9cb140d6d9")
    }
  ],
  createdAt: 2023-09-08T01:40:51.946Z,
  lastUpdatedAt: 2023-09-08T01:40:51.946Z,
  isDeleted: false,
  __v: 0
}

type or paste code here

type or paste code here

by trial and error, used projection: { ‘invitees’: { $slice: -1 } } … using any other parameter/element in projection breaks it…

thanks for the prompt response, appreciated.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.