我每小时都会向模式添加条目,以便跟踪当天的增长情况,同时保持当前的当前分数.现在我希望能够提取过去一周每天的最新记录.结果将是6个记录,在午夜或午夜左右,前6天,第7个记录是当天的最新记录.
这是我的架构:
var schema = new Schema({
aid: { type: Number }
, name: { type: String }
, score: { type: Number }
, createdAt: { type: Date, default: Date.now() }
})
Run Code Online (Sandbox Code Playgroud)
编辑
我已经尝试过使用这个静态,但它会将完全相同的记录拉7次
schema.statics.getLastWeek = function(name, fn) {
var oneday = 60 * 60 * 24
, now = Date.now()
, docs = []
for (var i = 1; i <= 7; i++) {
this.where('name', new RegExp(name, 'i'))
.where('createdAt')
.gte(now - (i * oneday))
.desc('createdAt')
.findOne(function(err,doc){
docs.push(doc)
}) …Run Code Online (Sandbox Code Playgroud)