title: Find duplicates with aggregation and lookup with MongoDB and Golang
body: <p>I need to find duplicates with aggregation and lookup with MongoDB and Golang.
  Here is my <code>Event</code> structure.</p>
  
  <pre><code>// Event describes the model of an Event
  type Event struct {
      ID            string      `bson:"_id" json:"_id" valid:"alphanum,printableascii"`
      OldID         string      `bson:"old_id" json:"old_id" valid:"alphanum,printableascii"`
      ParentID      string      `bson:"_parent_id" json:"_parent_id" valid:"alphanum,printableascii"`
      Name          string      `bson:"name" json:"name"`
      Content       string      `bson:"content" json:"content"`
      Slug          string      `bson:"slug" json:"slug"`
      LocationID    string      `bson:"_location_id" json:"_location_id"`
      Price         string      `bson:"price" json:"price"`
      CreatedBy     string      `bson:"created_by" json:"created_by"`
      CreatedAt     time.Time   `bson:"created_at" json:"created_at"`
      ModifiedAt    time.Time   `bson:"modified_at" json:"modified_at"`
  }
  </code></pre>
  
  <p>Here is the request I already have :</p>
  
  <pre><code>// Create the pipeline
      pipeline := []bson.M{
          bson.M{
              "$group": bson.M{
                  "_id": bson.M{
                      "_location_id": "$_location_id",
                      "start_date":   "$start_date",
                  },
                  "docs":  bson.M{"$push": "$_id"},
                  "count": bson.M{"$sum": 1},
              },
          },
          bson.M{
              "$match": bson.M{
                  "count": bson.M{"$gt": 1.0},
              },
          },
      }
  
      // Do the request
      dupes := []bson.M{}
      err := session.DB(shared.DatabaseNamespace).C(dao.collection).Pipe(pipeline).All(&amp;dupes)
  </code></pre>
  
  <p>The events must not have the same <strong>start_date</strong> and the same <strong>_location_id</strong>.
  This is what I can get :</p>
  
  <pre><code>/* 1 */
  {
      "_id" : {
          "_location_id" : "4okPZllaoueYC3U2",
          "start_date" : ISODate("2018-04-22T18:00:00.000Z")
      },
      "count" : 2.0,
      "docs" : [ 
          "FFSC2sJcrWgj2FsU", 
          "lwHknTHFfVAzB8ui"
      ]
  }
  
  /* 2 */
  {
      "_id" : {
          "_location_id" : "pC8rlLVao5c2CeBh",
          "start_date" : ISODate("2018-04-03T19:00:00.000Z")
      },
      "count" : 2.0,
      "docs" : [ 
          "jPRbkINiCExzh2tT", 
          "C8hx92QSZEl7HUIz"
      ]
  }
  </code></pre>
  
  <p>Fine, it is working, but..
  I would like to obtain, directly from Mongo, an array of my Event type, and if it is possible, an array of array of Event : <code>[][]*Event</code>.
  In order words, an array of the duplicates (between them).</p>
  
  <p>For example :</p>
  
  <pre><code>// Pipeline
  ...
  
  // Do the request
  events := [][]*Events
  err := session.DB(shared.DatabaseNamespace).C(dao.collection).Pipe(pipeline).All(&amp;events)
  </code></pre>
  
  <p>Or, do I need to perform the logic with Golang to achieve what I need ?</p>
  
  <p>The libraries I use are :</p>
  
  <pre><code>"gopkg.in/mgo.v2"
  "gopkg.in/mgo.v2/bson"
  </code></pre>
  
  <p>Note only : no need to take care about the <code>_location_id</code>, I lookup it with a DTO inside my Golang logic.</p>
  
  <p><strong>EDIT</strong> : If I cannot lookup the IDs, can I at least obtain the IDs as an array directly in the result ? For example :</p>
  
  <pre><code>[ 
      "jPRbkINiCExzh2tT", 
      "C8hx92QSZEl7HUIz"
  ]
  </code></pre>
  
  <p>This is what I tried to add to the request : <code>{$out: "uniqueIds"}</code>. But it is not working.</p>