【发布时间】:2014-10-23 10:03:57
【问题描述】:
我正在尝试使用 Mongoose Aggregate 方法转换这句话:
“对于每个具有给定 oid 的玩家,选择玩得最多的游戏”。 这是我的游戏架构:
gameSchema = new mongoose.Schema({
game_name:{type:String},
game_id:{type:String},
oid:{type: String},
number_plays:{type:Number,default:0},
})
Game = mongoose.model('Game', gameSchema);
这是我正在使用的代码:
var allids = ['xxxxx','yyyy'];
Game.aggregate([
{$match: {'oid': {$in:allids}}},
{$sort: {'number_plays': -1}},
{$group: {
_id: '$oid',
plays:{$push:"$number_plays"},
instructions:{$push:"$game_instructions"}
}}
], function(err,list){
console.log(list);
res.end();
});
上面的代码返回如下:
[ { _id: 'yyyy', plays: [ 10,4,5 ] },
{ _id: 'xxxxx',
plays: [ 28, 14, 10, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] } ]
问题在于它会返回所有游戏,而不是最常玩的游戏。所以我的问题是:是否可以限制 $group 中填充的字段?
【问题讨论】:
标签: node.js mongodb mongoose aggregation-framework