我有这个查询从我的集合中返回不同的测试ID
Model.find().distinct('TestId', (err, data) => { console.log(data); }
[1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]
这些所有值都有TestName
,现在我也要返回它们。像这样:
[{TestId:1,TestName:' tname'} ...]
我该怎么做?
答案 0 :(得分:1)
-save
不支持在输出中添加其他字段,但您可以使用简单的distinct
管道执行此操作:
aggregate
这会按Model.aggregate([{$group: {_id: '$TestId', TestName: {$first: '$TestName'}}}])
对文档进行分组,并在每个组的第一个文档中包含TestId
字段。