title: How to remove duplicates from mongoDB and keeping unique data using aggregate function
body: <p>Hi i have been trying to show an output of unique data without the duplicates in the Mongodb. </p>
  
  <p>Below is one sample of my entire document in my mongoDB</p>
  
  <pre><code>{
          "_id" : ObjectId("5a43aa19d4b45e362428e2da"),
          "comments_data" : {
                  "id" : "28011986676_10155780942281677",
                  "comments" : {
                          "paging" : {
                                  "cursors" : {
                                          "after" : "WTI5dGJXVnVkRjlqZAFhKemIzSTZAN4TlRBeE5EWXlPQT09",
                                          "before" : "WTI5dGJXVnVk4TVRZAMk56YzZANVFV4TlRBeE5EWXlPQT09"
                                  }
                          },
                          "data" : [
                                  {
                                          "created_time" : "2018-01-03T21:23:47+0000",
                                          "message" : "Poor customer care service after became the Singtel customer.I did my          re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service.",
                                          "from" : {
                                                  "name" : "Sundararaju G",
                                                  "id" : "1020391"
                                          },
                                          "id" : "10155780942281677_10155811924116677"
                                  }
                          ]
                  }
          },
          "post_id" : "28011986676_10155780942281677",
          "post_message" : "\"Singtel TV celebrated our 10th birthday with 10 awesome experiences for our customers! Each of our winners won a trip of a lifetime - from attending the Emmy Awards, getting a magical princess treatment at Disneyland, to catching a Premier League game live in London! We thank all our customers for your support and we look forward to more great years to come!\"",
          "reactions_data" : {
                  "reactions" : {
                          "paging" : {
                                  "cursors" : {
                                          "after" : "TVRBd01EQXpNVEF5T1Rje4TXc9PQZDZD",
                                          "before" : "TVRjNE56TTBBek56a3hNek14TWc9PQZDZD"
                                  },
                                  "next" : "https://graph.facebook.com/v2.7/280119866761677/reactions?access_token=EAA"
                          },
                          "data" : [
                                  {
                                          "type" : "ANGRY",
                                          "id" : "1020573391",
                                          "name" : "Sundararaju Gh"
                                  },
                                  {
                                          "type" : "LIKE",
                                          "id" : "64721496",
                                          "name" : "Zhiang Xian"
                                  }
                          ]
                  },
                  "id" : "28011986676_102281677"
          }
  }
  </code></pre>
  
  <p>after that i have been trying to extract out the message comments at the comments_data field.
  i have tried using this query</p>
  
  <pre><code>db.sInsert.find({post_id: "28011986676"}, {post_id:1, 'comments_data.comments.data.message':1})
  </code></pre>
  
  <p>where the results i get is all duplicates data.</p>
  
  <pre><code>{ "_id" : ObjectId("5a43aa19d4b45e362428e2ec"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
  { "_id" : ObjectId("5a4660f2d4b45e3698398320"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
  { "_id" : ObjectId("5a47ae92d4b45e2148941901"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
  { "_id" : ObjectId("5a4928b1d4b45e208cfd6916"), "comments_data" : { "comments" : { "data" : [ { "message" : "Who else loves Apple ?" } ] } }, "post_id" : "28011986676" }
  </code></pre>
  
  <p>Thereafter, i have tried using the aggregate function inside the MongoDB</p>
  
  <pre><code>db.sInsert.aggregate([   { $match: {post_id: {"$eq": "28011986676" } } },   { $project: {'message': '$comments_data.comments.data.message', _id:0} }, ])
  </code></pre>
  
  <p>The result i have get is </p>
  
  <pre><code>{ "message" : [ "Who else loves Apple ?" ] }
  { "message" : [ "Who else loves Apple ?" ] }
  { "message" : [ "Who else loves Apple ?" ] }
  { "message" : [ "Who else loves Apple ?" ] }
  </code></pre>
  
  <p>How do i get unique result by using the aggregate function ? I also have tried using ensureIndex, it still does not helps in returning unique results from the mongoDB.</p>