对于以下 Schema(伪代码)。
new mongoose.Schema({
name: {type: String, unique: true},
foo: {type: Number},
bar: {type: Number, default: null},
}
Run Code Online (Sandbox Code Playgroud)
正常结果是.find():
[{
_id: x
name: 'Name1'
foo: 1
bar: 222
}, {
_id: y
name: 'Name2'
foo: 1
bar: 333
}, {
_id: z
name: 'Name3'
foo: 2
bar: 444
}];
Run Code Online (Sandbox Code Playgroud)
我想要返回按 foo 分组的结果,键应该是 foo 的变量/数量:
[{
'1': [{
name: 'Name1'
bar: 222
}, {
name: 'Name2'
bar: 333
}],
'2': [{
name: 'Name3'
bar: 444
}]
}]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用聚合,但我永远无法用“Foo 的数量”替换 _id。
对于猫鼬来说这可能吗?或者我需要在 …