设为首页 加入收藏

TOP

MongoDB用时间筛选_id字段
2016-01-29 17:05:13 】 浏览:7943
Tags: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万数据。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ocp-360 下一篇ocp-337

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目