MongoDB 用时间淘选_id字段

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

这样就找出来2015/10/01之前的779万数据。



版权声明:本文为博主原创文章,未经博主允许不得转载。