基于外键的Mongoose查询

时间:2017-11-04 05:14:51

标签: node.js mongodb mongoose

离开架构

mongoose.Schema({
    _id: String
});

状态架构

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

状态架构中的 LeaveId _id 保留架构的引用。您可能已经猜到离开状态有一对多的关系。

现在,我想找到状态的列表,其中只包含最新状态 离开

示例
离开收藏

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

状态收集

[
    {
        _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
    }
]

预期产出

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

0 个答案:

没有答案