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

猜你喜欢

转载自blog.csdn.net/chenyulancn/article/details/83585860