title: Project nested embedded document in mongodb aggregate
body: <p>I have a nested embedded document which looks something like this. Each post has n-comments and each comment has a user detail with name and email id.</p>
  
  <p>I want to project just the name of the commented user into the list</p>
  
  <pre><code>{
      "PostId":"Post001",
      "Comments":[
           {"_id": "001",
            "CommentedBy":{
              "_id":"User001",
              "Name":"UserName001",
              "email":"user001@eg.com"
              }
           },
           {"_id": "002",
             "CommentedBy":{
              "_id":"User002",
              "Name":"UserName002",
              "email":"user001@eg.com"
              }
           },
           {"_id": "003",
            "CommentedBy":{
              "_id":"User003",
              "Name":"UserName003",
              "email":"user001@eg.com"
              }
           }
      ]
  }
  </code></pre>
  
  <p>And I want to transform into something that looks like this, by using mongodb's aggregation pipeline.</p>
  
  <pre><code>{
      "PostId":"Post001"
      "Comments":[
           {"_id": "001",
            "CommentedBy":"UserName001",
           },
           {"_id": "002",
             "CommentedBy": "UserName002"
           },
           {"_id": "003",
            "CommentedBy": "UserName003"
           }
      ]
  }
  </code></pre>
  
  <p>Using mongo's projection query provides a list of <code>CommentedBy</code> with all the names in it. <strong>How can I achieve this using mongo's aggregate query. Is there a way to do it without using <code>$unwind</code>.</strong></p>
  
  <p>Query I tried and the result I got. </p>
  
  <pre><code>db.getCollection('post').aggregate([
  {$project:{"Comments.CommentedBy":"$Comments.CommentedBy.Name"}}
  ])
  
  {
      "_id" : ObjectId("5b98b4cc3bb8c65aeacabd78"),
      "Comments" : [ 
          {
              "CommentedBy" : [ 
                  "UserName001", 
                  "UserName002", 
                  "UserName003"
              ]
          }, 
          {
              "CommentedBy" : [ 
                  "UserName001", 
                  "UserName002", 
                  "UserName003"
              ]
          }, 
          {
              "CommentedBy" : [ 
                  "UserName001", 
                  "UserName002", 
                  "UserName003"
              ]
          }
      ]
  }
  </code></pre>