首页 > 解决方案 > 使用 mongoose 5.x 按条件和 groupby 外键查找

问题描述

我正在尝试查询一个集合,我只想要满足 createdAt 条件的结果,然后我想通过 groupId 对这些结果进行分组

我尝试使用 $match 和 $group,但我收到一条错误消息,提示我的 foreignKey (groupId) 未定义

Call.aggregate([
{
    $match: {
        createdAt: {
            $gte: "2019-04-24T00:00:00.000Z",
            $lt: "2019-05-01T00:00:00.000Z"
        }
    }
},
{
    $group: {
        _id: groupId
    }
}
]).exec().then(result => {

 result.forEach(function (call) {
        console.log(call.createdAt);
    });

return res.status(200).json({
results: result});

});

标签: javascriptnode.jsmongodbmongoose

解决方案


推荐阅读