2017-03-15 43 views
0

如何返回表中唯一值的计数?这将返回一组唯一的ID ...node-orm2获取唯一出现次数?

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 

我看到如何聚集和我见过怎么算(),但我不明白,我可以做他们两个在一起。

回答

0

我能够用得到预期的结果...

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

哪个返回...

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