0

これらのコレクション、統計、アイテムがあります。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' } } }
)
4

1 に答える 1

0

$projectパイプラインの一部は必要ありません。$groupステージから必要なものを取得する必要があります

db.stats.aggregate({ $group: { _id: "$item.type" , total: { $sum: '$hourly' } } });
于 2015-02-02T22:11:07.500 に答える