MongoDB 用时间筛选_id字段

来源:互联网 发布:两进一出网络切换器 编辑:程序博客网 时间:2024/05/22 11:36

下面的代码来自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万数据。



0 0
原创粉丝点击