我想创建一个关于每分钟存储多少新文档的统计数据。
由于具有标准 ObjectID 的 _id 字段已经包含文档创建的时间戳,我认为应该可以以某种方式使用它。
在 Stackoverflow 上,我发现以下 map reduce 代码可以在有用于创建数据的专用字段时完成它
Map-Reduce count 每分钟 MongoDB 的文档数
map = function() {
var created_at_minute = new Date(this.created_at.getFullYear(),
this.created_at.getMonth(),
this.created_at.getDate(),
this.created_at.getHours(),
this.created_at.getMinutes());
emit(created_at_minute, {count: 1});
}
reduce = function(key, values) {
var total = 0;
for(var i = 0; i < values.length; i++) { total += values[i].count; }
return {count: total};
}
根据 Mongo DB 文档(http://docs.mongodb.org/manual/reference/object-id/),应该可以通过调用 ObjectId("507f191e810c19729de860ea").getTimestamp() 从 _id 获取时间戳。
现在我不知道是否可以在 map 函数中使用这个 getTimestamp() 。
有没有人知道怎么做或者有更好的方法吗?
我需要它可以在 python 或 php 中实现