1

Leave Schema

mongoose.Schema({
    _id: String
});

Status Schema

mongoose.Schema({
    _id: String,
    LeaveId:  String,
    Time: Date
});

Here LeaveId in Status schema is the reference of _id for Leave Schema. You might have guessed Leave and Status have one to many relationship.

Now I want to find the list of status which contains only the latest status for each leave.

Example
Leave Collection

[
    {
        _id: '<id-1>'
    },
    {
        _id: '<id-2>'
    }
]

Status Collection

[
    {
        _id: '<id-3>',
        LeaveId: '<id-1>',
        Time: 2013-0-0
    },
    {
        _id: '<id-4>',
        LeaveId: '<id-1>',
        Time: 2014-0-0
    },
    {
        _id: '<id-5>',
        LeaveId: '<id-2>',
        Time: 2016-0-0
    },
    {
        _id: '<id-6>',
        LeaveId: '<id-2>',
        Time: 2015-0-0
    }
]

Expected Output

[
    {
        _id: '<id-4>',
        LeaveId: '<id-1>',
        Time: 2014-0-0
    },
    {
        _id: '<id-5>',
        LeaveId: '<id-2>',
        Time: 2016-0-0
    }
]
  • Also `LeaveId: String` is just dead wrong. This relates to the `_id` in the `Leave` collection, and that property is an `ObjectId`. Both need to be the same type for any kind of referenced "join". – Neil Lunn Nov 04 '17 at 05:21
  • @NeilLunn, Both are of string type – Rahim Ahmmed Nov 04 '17 at 05:25
  • @NeilLunn, the question you referred just solves this problem partially. Could you have another look? – Rahim Ahmmed Nov 04 '17 at 05:30
  • It solves it completely. Do you think you could be quiet and actually spend more than 5 minutes understanding it? From my perspective, if you had to ask the question at all then you have not understood the answers given already in such a short space of time. – Neil Lunn Nov 04 '17 at 05:32

0 Answers0