【问题标题】:node-orm2 get count of unique occurrences?node-orm2 获取唯一出现次数?
【发布时间】:2017-03-15 19:03:35
【问题描述】:

如何返回表中唯一值的计数?这将返回一个唯一 ID 数组...

req.models.pages.aggregate().distinct("page").get(function (err, page) {...
// returns [ '6', '92', '90', '91', '93', '94', '102' ]
// the unique ids of the individual pages

但是,如何返回具有相应计数的对象?比如……

{ '6': 2, '92':7, '90':12, etc...}
// the unique page ids with their associated counts

我了解了如何聚合,也了解了如何 count(),但我不知道在哪里可以同时进行。

【问题讨论】:

    标签: node.js orm node-orm2


    【解决方案1】:

    我能够使用...获得预期的结果

    req.models.pages.aggregate(['page']).count().groupBy('page').get(function (err, pages) {...
    

    返回...

    [
      {
        "page": "6",
        "count": 2
      },
      {
        "page": "92",
        "count": 7
      },
      {
        "page": "90",
        "count": 12
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2010-10-24
      相关资源
      最近更新 更多