2017-03-15 45 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 
    } 
]