MongoDB - Projecting elements in the list as document using $unwind

Let us see how we can use $unwind to display list of elements as documents. For example for a given order_id if we have a list which contain 3 elements, unwind will return 3 documents.

  • Connect to mongo server
  • Switch to retail database
  • Make sure there is a denormalized collection order_details.
  • Run unwind to convert list into regular documents.
  • order_item_details will be nested document for each and every document for given order_id
db.orderDetails.aggregate([
  {
      $unwind: "$order_item_details"
  }
]).pretty()