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"),
      }
    ]
}
2
  • You'd better use $unwind to unfold the array and $match to filter out the required "leave" from there on Commented Sep 13, 2016 at 12:32
  • Thanks.Implemented and got the desired result.Will post the answer now.Thanks Commented Sep 15, 2016 at 2:17

1 Answer 1

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++
    }
}       
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.