加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell – 如何从Mongo ObjectID中提取创建的日期

发布时间:2020-12-15 16:28:44 所属栏目:安全 来源:网络整理
导读:我使用Mongo shell来查询我的Mongo数据库。我想使用ObjectID中包含的时间戳作为我的查询的一部分,也作为一个列提取到输出。我已经设置Mongo自己创建ObjectIDs。 我的问题是我找不到如何使用ObjectID提取其时间戳。 这里是我想要工作的查询。 ‘createdDate
我使用Mongo shell来查询我的Mongo数据库。我想使用ObjectID中包含的时间戳作为我的查询的一部分,也作为一个列提取到输出。我已经设置Mongo自己创建ObjectIDs。

我的问题是我找不到如何使用ObjectID提取其时间戳。

这里是我想要工作的查询。 ‘createdDate’字段是一个占位符;不确定正确的字段是:

//Find everything created since 1/1/2011
db.myCollection.find({date: {$gt: new Date(2011,1,1)}});

//Find everything and return their createdDates
db.myCollection.find({},{createdDate:1});
getTimestamp()

你需要的功能是这一个,它已经包含在你已经在shell中:

ObjectId.prototype.getTimestamp = function() {
    return new Date(parseInt(this.toString().slice(0,8),16)*1000);
}

参考文献

从文档中查看此部分:

> Extract insertion times from _id rather than having a separate timestamp field

这个单元测试也演示了同样的:

> mongo / jstests / objid6.js

使用Mongo shell的示例:

> db.col.insert( { name: "Foo" } );
> var doc = db.col.findOne( { name: "Foo" } );
> var timestamp = doc._id.getTimestamp();

> print(timestamp);
Wed Sep 07 2011 18:37:37 GMT+1000 (AUS Eastern Standard Time)

> printjson(timestamp);
ISODate("2011-09-07T08:37:37Z")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读