loopbackjs - 环回查询 - 如何分组?

标签 loopbackjs

有没有办法按过滤器执行分组?
类似于以下内容:

 model.find({groupBy: 'myField'})

我正在使用 PostgreSQL。

最佳答案

基于 this page您可以执行 native SQL 查询,但是,如果它仍然像他们所说的那样工作,我们没有关于此的更新:

Warning: This feature has not been fully tested and is not officially supported: the API may change in future releases. In general, it is always better to perform database actions through connected models. Directly executing SQL may lead to unexpected results, corrupted data, and other issues.



对于数据源 > 1.x
dataSource.connector.execute(sql_stmt, params, callback);

或 =< 1.x
dataSource.connector.query(sql_stmt, params, callback);

1.sql_stmt - SQL 字符串。

2.params - SQL 语句的参数。

如果您有一个参数化查询,例如:
SELECT * FROM TABLE WHERE id = $1 or SOMETHING = $2

参数将是:params = [358, "SOMETHING"];
3.callback - 回调函数

4.示例
module.exports = function (Collection) {    
    var ds = Collection.dataSource;
    var sql = "SELECT _id FROM table GROUP BY my_field;";
    var params = []; // = null (could be null also)

    ds.connector.execute(sql, params, function (err, data) {

        if (err) console.error(err);

        console.log(data);

    });    
};

如果 queryexecute不工作尝试 executeSQL

关于loopbackjs - 环回查询 - 如何分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39075918/

相关文章:

node.js - 扩展用户和访问 token 模型时不生成访问 token - Loopback 3

javascript - 为什么此方法中的回调位于 thenable 中,回调不应该是 updateAll 方法返回语句的一部分吗?

node.js - 如何在环回中验证/授权快速路由

postgresql - 使用 Postgres 的 StrongLoop 查询/存储过程?

node.js - Loopback 远程 Hook 中的下一个参数是什么?

node.js - SLC环回: Using model instance from within a model hook

angularjs - 如何在 postgresql 中存储 Oauth 响应?

javascript - Loopback 模型是否有带有 where 子句的 "exists"方法?

javascript - 尝试访问环回相关模型查询时 JS 对象的奇怪行为

angularjs - StrongLoop Loopback Yeoman Angular