阅读背景:

MongoDB 用时间筛选_id字段

来源:互联网 

下面的代码来自stackoverflow,在MongoDB shell中运行

> function objectIdWithTimestamp(timestamp) {
     // Convert string date to Date object (otherwise assume timestamp is a date)
     if (typeof(timestamp) == "string") {
         timestamp = new Date(timestamp);
     }
 
     // Convert date object to hex seconds since Unix epoch
     var hexSeconds = Math.floor(timestamp/1000).toString(16);
 
     // Create an ObjectId with that hex timestamp
     var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
 
     return constructedObjectId
 }
> db.digital_message.find({ _id: { $lt: objectIdWithTimestamp("2015/10/01") } }).count()
7792766
> db.digital_message.count()
7959508
> function




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: