【问题标题】:groupby with orm nodejs on postgresql在 postgresql 上使用 orm nodejs 的 groupby
【发布时间】:2015-11-03 18:42:07
【问题描述】:

你好,我在使用 ORM(node-orm2) 和数据库 postgresql 的 nodejs 中选择 groupBy 时遇到问题。

代码:

req.models.roomcategorylog.find(["id", "Z"],
function(err, results) {
if (err) {
    res.json({
        status: false,
        message: 'Error found',
        data:err
    });
} else {
    res.json({
        status:true,
        message: 'Data found',
        data:results
    });
    // console.log(results);
}
});

这段代码的结果是这样的:

id , users_id , username
37  1          faris
36  1          faris
35  1          faris
34  2          ridho
33  3          kotaro

但我在我的系统中需要的是 1 个“users_id”,例如查询结果中的分组:

id , users_id , username
37  1          faris
34  2          ridho
33  3          kotaro

感谢您的关注和解决方案:)

【问题讨论】:

    标签: node.js postgresql node-orm2


    【解决方案1】:

    你可能想使用这样的东西:

    req.models.roomcategorylog
        .aggregate(["username", "users_id"], { id: "Z" })
        .groupBy("username").get(function(err, results) {
             // results should be your grouped data
        })
    

    【讨论】:

    • 我怎样才能得到聚合?在我的结果中说“无效的错误代码:缺少聚合函数”。谢谢之前
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2019-07-11
    • 2020-03-06
    • 2019-01-22
    • 2014-04-17
    • 2022-01-21
    相关资源
    最近更新 更多