+-
mongoose 如何实现查询数据按照字段分组?

问题:
通过find()查询到数据如下,如何根据字段type来进行分组呢?

{ "id":"01", "type":"1001", "title":"测试1" }, { "id":"02", "type":"1002", "title":"测试2" }, { "id":"03", "type":"1002", "title":"测试3" },

期望分组后的查询结果是按照type字段来分组的数据。

用Lodash吧,很方便的:

var _ = require('lodash'); _.groupBy(array, "type")

希望能帮助到你。