0

I'm using golang in the backend and mongoDB as database. When I try to retrieve documents from one of the embedded array I'm getting only one index of the embedded array in the result as the result.

My struct is like this

type (
    Employee struct {
        Name           string
        EmpId          string
        Password       string
        Leave        []*LeaveInfo
    }
LeaveInfo struct {

        Id              int
        Days            float64
        From            time.Time
        To              time.Time
        AppliedDate     time.Time
        Status          string
        ApprovedDate    time.Time
    }

My golang code is

   t := time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC)
    var result []*Employee
        if e := c.Find(nil).Select(bson.M{"leave": bson.M{
            "$elemMatch": bson.M{
                "from": bson.M{
               "$gte":t, 
    },
            },
        },
        },
        ).All(&result); e != nil {
            fmt.Println(e)
}

My database structure is

_id:57d7a6673897593ae84bed49{
Name:"Mark"
EmpId:"E123"
Password:1234
Leave:[
{
    "id" : 0,
    "days" : 1.5,
    "from" : ISODate("2016-12-01T00:00:00Z"),
    "to"   : ISODate("2016-12-02T12:00:00Z"),
    "applieddate" : ISODate("0001-01-01T00:00:00Z"),
    "status" : "Approved",
    "approveddate" : ISODate("0001-01-01T00:00:00Z"),

  },
{

    "id" : 1,
    "days" : 2.0,
    "from" : ISODate("2016-12-11T00:00:00Z"),
    "to" : ISODate("2016-12-12T00:00:00Z"),
    "applieddate" : ISODate("0001-01-01T00:00:00Z"),
    "status" : "Approved",
    "approveddate" : ISODate("0001-01-01T00:00:00Z"),

  },
]
}

In this I m getting only the index 0 of the Leave array.As you can see both the index of the array have date greater than t.But when i retreive it I m getting only one index (index 0).But what i need is to retreive all the index which has date greater than t.Please help me.thanks

My result is as follows

{
    "Name": "",

    "EmpId": "",
    "Password": "",
"Leave": [
      {

        "Id": 0,
        "Days": 1.5,
        "from" : ISODate("2016-12-01T00:00:00Z"),
        "to"   : ISODate("2016-12-02T12:00:00Z"),
        "applieddate" : ISODate("0001-01-01T00:00:00Z"),
        "status" : "Approved",
        "approveddate" : ISODate("0001-01-01T00:00:00Z"),
      }
    ]
}
Joundill
  • 6,828
  • 12
  • 36
  • 50
Rajesh Kumar
  • 207
  • 1
  • 6
  • 12

1 Answers1

1

I am not sure that what you ask is possible in a single mongo query. I would rather get all elements and the filter in go with something like this:

var result []*Employee
err := c.Find(nil).All(&result)
if err != nil {
    // do stuff...
}

// For each result, filter the Leave values having a to small From value.
t := time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC)
for i := range result {
    var j int
    for j < len(result[i].Leave) {
        if result[i][j].From.Before(t) {
            result[i] = append(result[i][:j], result[i][j+1:]...)
            continue
        }
        j++
    }
}       
  • Thanks for your reply.I found that by implementing the $unwind,$match,$project this can be implemented.Now i have got the result that i need.Once Again thanks for your effort.....I will post my answer now.Thanks – Rajesh Kumar Sep 15 '16 at 02:20