これらのコレクション、統計、アイテムがあります。Stats には、サブドキュメントとして 1 つの項目があります。
var ItemSchema = new Schema({
type: String,
created_at: Date,
updated_at: {
type: Date,
default: Date.now()
}
});
var StatsSchema = new Schema({
item: {
type: Schema.ObjectId,
ref: 'Item'
},
url: String,
date: Date,
action: String,
hourly: Number
});
item.type ごとに Stats グループを集計したいと思います。出来ますか?
私はこのようなことを試しましたが、運がありませんでした:
db.stats.aggregate(
{ $project: { _id: 1, hourly: 1, action: 1, item: 1, type: '$item.type' } },
{ $group: { _id: { action: '$action', type: '$item.type' }, total: { $sum: '$hourly' } } }
)