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
}
]