我有一个收集用户每日统计信息的集合。因此,每天每位用户都会根据当前日期插入一个文档。
const statSchema = new Schema({
userId: {
type: String,
ref: 'User',
required: true
},
date: {
type: Date,
default: new Date(new Date().setUTCHours(0, 0, 0, 0)),
},
profileViewsCount: {type: Number, default: 0},
productsViewsCount: {type: Number, default: 0},
});
我正在使用以下几行代码来获取用户今天的统计信息
statSchema.statics.getStats = async function (userId) {
const date = new Date(new Date().setUTCHours(0, 0, 0, 0));
const stats = await this
.findOne({userId, date});
console.log('stats', stats);
return stats;
};
但是我希望获得包括今天,昨天,周的统计信息,因此,计数将相应地相加并得出如下结果。 / p>
today: {
profileViewsCount: 123,
productsViewsCount: 670,
},
yesterday: {
profileViewsCount: 8865,
productsViewsCount: 9090,
},
week: {
profileViewsCount: 58656,
productsViewsCount: 234234,
}
我还没有找到一种方法,任何帮助将不胜感激。