How to Unwind / populate an Array in Mongoose Aggregate Pipeline

I’m working with Mongoose and trying to unwind/populate the comments.attachments field within an aggregation pipeline. Here’s the relevant portion of my code:

`const result = await Ticket.aggregate([
         { $match: { $expr: { $eq: ['$_id', { $toObjectId: "667293eca3a67d7c5d9ff1e2" }] } } },
        { $unwind: '$comments' },
       { $match: { $expr: { $eq: ['$comments._id', { $toObjectId: "667382f627df37eb4e7a3220" }] } } },
 ])`

This query successfully retrieves a ticket document and unwinds the comments array. However, the comments.attachments field remains an empty array even though there are attachments associated with the comment.

[ { _id: new ObjectId('667293eca3a67d7c5d9ff1e2'), referenceNo: 'TCK_06-19-2024_0001', subject: new ObjectId('6670dad080ea03a681f70295'), description: 'pero ang totoo, di bali na ako. ikaw lang iniisip ko.', priority: 'medium', status: 'open', initiator: new ObjectId('6670da9d80ea03a681f70292'), createdBy: new ObjectId('667106fff8abeeb0d87341ba'), assignedTo: null, attachments: [ [Object], [Object] ], comments: { userId: new ObjectId('6670db4180ea03a681f7029e'), content: '2nd comment w atta', attachments: [Array], _id: new ObjectId('667382f627df37eb4e7a3220'), createdAt: 2024-06-20T01:16:38.190Z, updatedAt: 2024-06-20T01:16:38.190Z }, createdAt: 2024-06-19T08:16:44.129Z, updatedAt: 2024-06-20T02:14:51.282Z, __v: 0 } ]

Here are my models for reference:

Ticket Model

`const mongoose = require('mongoose');
 const Schema = mongoose.Schema;
 const ticketCommentSchema = require('./ticketCommentModel');
 const ticketAttachmentSchema = require('./ticketAttachmentModel');

 const ticketSchema = new Schema({
   // ... other schema properties
     comments: [ticketCommentSchema],
    attachments: {
     type: [ticketAttachmentSchema],
    required: true,
     validate: [attachmentsArray => attachmentsArray.length > 0, 'At least one attachment is   required']
   },
   }, {
     timestamps: true
   });

    module.exports = mongoose.model('Ticket', ticketSchema);`

Ticket Comment Model

`const mongoose = require('mongoose');
 const Schema = mongoose.Schema;
 const ticketAttachmentSchema = require('./ticketAttachmentModel');

 const ticketCommentSchema = new Schema({
   userId: {
     type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
     required: true
    },
     content: {
       type: String,
      trim: true,
       required: true
      },
       attachments: [ticketAttachmentSchema]
      }, {
      timestamps: true
      });

     module.exports = ticketCommentSchema;`

Ticket Attachment Model

`const mongoose = require('mongoose');
 const Schema = mongoose.Schema;

 const ticketAttachmentSchema = new Schema({
  // ... attachment schema properties
  }, {
    timestamps: true
       });

 module.exports = ticketAttachmentSchema;`

My question is:

How can I modify the aggregation pipeline to unwind or populate the comments.attachments field and retrieve the attachment details within the query results?

I’ve searched online for solutions, but haven’t found anything specific to this scenario. Any help would be greatly appreciated!

I try this aggregation pipeline

`const result = await Ticket.aggregate([
        { $match: { $expr: { $eq: ['$_id', { $toObjectId: "667293eca3a67d7c5d9ff1e2" }] } } },
        { $unwind: '$comments' },
        { $match: { $expr: { $eq: ['$comments._id', { $toObjectId: "667382f627df37eb4e7a3220" }] } } },
        {
            $unwind: {
                path: "$comments.attachments",
                preserveNullAndEmptyArrays: true
            }
        },
       ])`

and it returns this data

`[
     {
_id: new ObjectId('667293eca3a67d7c5d9ff1e2'),
referenceNo: 'TCK_06-19-2024_0001',
subject: new ObjectId('6670dad080ea03a681f70295'),
description: 'pero ang totoo, di bali na ako. ikaw lang iniisip ko.',
priority: 'medium',
status: 'open',
initiator: new ObjectId('6670da9d80ea03a681f70292'),
createdBy: new ObjectId('667106fff8abeeb0d87341ba'),
assignedTo: null,
attachments: [ [Object], [Object] ],
comments: {
  userId: new ObjectId('6670db4180ea03a681f7029e'),
  content: '2nd comment w atta',
  attachments: [Object],
  _id: new ObjectId('667382f627df37eb4e7a3220'),
  createdAt: 2024-06-20T01:16:38.190Z,
  updatedAt: 2024-06-20T01:16:38.190Z
},
    createdAt: 2024-06-19T08:16:44.129Z,
   updatedAt: 2024-06-20T02:14:51.282Z,
    __v: 0
    },
  {
     _id: new ObjectId('667293eca3a67d7c5d9ff1e2'),
      referenceNo: 'TCK_06-19-2024_0001',
subject: new ObjectId('6670dad080ea03a681f70295'),
description: 'pero ang totoo, di bali na ako. ikaw lang iniisip ko.',
priority: 'medium',
status: 'open',
initiator: new ObjectId('6670da9d80ea03a681f70292'),
createdBy: new ObjectId('667106fff8abeeb0d87341ba'),
assignedTo: null,
attachments: [ [Object], [Object] ],
comments: {
  userId: new ObjectId('6670db4180ea03a681f7029e'),
  content: '2nd comment w atta',
  attachments: [Object],
  _id: new ObjectId('667382f627df37eb4e7a3220'),
  createdAt: 2024-06-20T01:16:38.190Z,
  updatedAt: 2024-06-20T01:16:38.190Z
      },
createdAt: 2024-06-19T08:16:44.129Z,
updatedAt: 2024-06-20T02:14:51.282Z,
 __v: 0
           }
        ]`

it turns comment.attachments to object but still doesn’t populate the data of attachments,
also the comment have 3 attachments.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật