title: to find the last index of the array in mongodb
body: <p>In the back end i m using go lang and for database i use mongoDB. I m trying to find the last document inserted in the embedded array so i can retrieve the document in the last array index without knowing its index.Right now i m getting all the documents of the employee and then find the last index.It is like overloading my RAM as i need to retrieve 1000 of record of employee and store it in ram before finding the last index of the array
  My struct is as follows</p>
  
  <pre><code> type (
          Employee struct {
              Name               string
              Password           string
              EmpId              string 
             EmailAddress       string
             Position           string
             Gender             string
             Nationality        string
             Department         string
             MaritalStatus      string
             Approvedby         string
             JoinDate           time.Time
             ConfirmationDate   time.Time
             EndDate            time.Time
              Leave             []*LeaveInfo  
          }
          LeaveInfo struct {
              Total        float64
              Id           int
              Days         float64
              From        time.Time
               To          time.Time  
              Status       string
              Certificate  []*CertificateInfo
          }
          CertificateInfo struct {
              FileName string
              FileType string
              FileSize int
  
          }
  </code></pre>
  
  <p>Here is how i do in my application </p>
  
  <pre><code> My code is follows
  
      employee := Employee{}
      err = c.Find(bson.M{
                    "empid": "1234"
                   }).One(&amp;result)
      if err != nil {
              log.Fatal(err)
      }
            name:=result.Name        
            lastindex:= result.LeaveInfo[len(result.LeaveInfo)-1].Id
  </code></pre>
  
  <p>As you can see  i retrive the whole employee data and then find the Id of the last document.Is there any better way to do this.Appreciate any help.Please ...Thanks..</p>
  
  <blockquote>
    <p>Newly Added Codes</p>
  </blockquote>
  
  <pre><code> This code is based on your example
  
     var result bson.M
  
      query := bson.M{"empid": "1234"}  // gets the employee you are interested in
      match := bson.M{"$match": query}  // Set up the match part of the pipeline
      unwind := bson.M{"$unwind": "$leave"}  // sets up the leave field to be unwound
  
      pipeline := []bson.M{match, unwind,{
      "$project":bson.M{
             "ID":bson.M{
                  "$slice": []interface{}{"$leave.id", -1},
          }
         }
  
  
      iter := postCollection.Pipe(pipeline).Iter()
      for iter.Next(&amp;result) {
          fmt.Printf("%+v\n", result)
      }
      iter.Close()
  </code></pre>
  
  <p>This code gives me lot of same document  like 542 documents .But all these document are same...</p>