Mongoose unwind Mongoose unwind mongoose mongoose

Mongoose unwind


An example solution would $unwind and $group the elements back but in a different way. For example:

db.getCollection('orders').aggregate([  // Unwind on "orderItems".  {$unwind: '$orderItems'},  {$group: {    // Group by "orderItems.productDispatchStatus".    _id: {_id: '$_id', productDispatchStatus: '$orderItems.productDispatchStatus'},    orderItems: {$push: '$orderItems'},    // Add all other fields.    orderStatus: {$first: '$orderStatus'},    orderNumber: {$first: '$orderNumber'}  }},  {$project: {    // Map "_id" back to the original.    _id: '$_id._id',    // Add all other fields.    orderStatus: 1,    orderItems: 1,    orderNumber: 1  }}])