我有一个升降机架构,其中包含两个关键信息 - 升力(长凳,硬拉,下蹲等)以及重量(重量提升)。
我想知道的是如何找到每个升降机的最大重量。
我可以使用升降机为给定用户填充数组,但不知道如何进一步降低结果,可以在同一个查询中完成吗?
var lift = new schema({
uID: { type: String },
lift: { type: String },
weight: { type: Number },
measurement: { type: String },
date: { type: Date, default: Date.now },
gear: [{type: String}]
});
我正在使用Mongoose和Node.js以及Express。
答案 0 :(得分:1)
这样的事情(在具有聚合框架的MongoDB shell上):
db.so.aggregate( [
{ $group: {
_id: '$lift',
max: { $max: '$weight' }
} }
] );
使用以下输入(删节):
db.so.insert( { lift: 'bench', weight: 80 } );
db.so.insert( { lift: 'bench', weight: 40 } );
db.so.insert( { lift: 'bench', weight: 76 } );
db.so.insert( { lift: 'squat', weight: 76 } );
db.so.insert( { lift: 'squat', weight: 72 } );
db.so.insert( { lift: 'squat', weight: 172 } );
db.so.insert( { lift: 'deadlift', weight: 142 } );
输出:
{
"result" : [
{
"_id" : "deadlift",
"max" : 142
},
{
"_id" : "squat",
"max" : 172
},
{
"_id" : "bench",
"max" : 80
}
],
"ok" : 1
}
在node.js中,您将更改:
db.so.aggregate( [
{ $group: {
_id: '$lift',
max: { $max: '$weight' }
} }
] );
使用:
collection.aggregate( [
{ $group: {
_id: '$lift',
max: { $max: '$weight' }
} }
], function(err, result) {
} );