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"),
}
]
}