mongodb 如何从 Mongo ObjectID 中提取创建日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7327296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract the created date out of a Mongo ObjectID
提问by emilebaizel
I'm using the Mongo shell to query my Mongo db. I want to use the timestamp contained in the ObjectID as part of my query and also as a column to extract into output. I have setup Mongo to create ObjectIDs on its own.
我正在使用 Mongo shell 来查询我的 Mongo 数据库。我想使用 ObjectID 中包含的时间戳作为查询的一部分,并作为一列提取到输出中。我已将 Mongo 设置为自行创建 ObjectID。
My problem is I can not find out how to work with the ObjectID to extract its timestamp.
我的问题是我不知道如何使用 ObjectID 来提取其时间戳。
Here are the queries I am trying to get working. The 'createdDate' field is a placeholder; not sure what the correct field is:
以下是我试图开始工作的查询。'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});
回答by Chris Fulstow
getTimestamp()
获取时间戳()
The function you need is this one, it's included for you already in the shell:
你需要的函数就是这个,它已经包含在 shell 中了:
ObjectId.prototype.getTimestamp = function() {
return new Date(parseInt(this.toString().slice(0,8), 16)*1000);
}
References
参考
Check out this section from the docs:
从文档中查看此部分:
This unit test also demostrates the same:
此单元测试也演示了相同的内容:
Example using the Mongo shell:
使用 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")
回答by Stefan
This questionis helpful to understand of how to use the _id's embedded timestamp in query situations (refers to the Mongo Extended JSONdocumentation). This is how it's done:
这个问题有助于理解在查询情况下如何使用_id的内嵌时间戳(参考Mongo Extended JSON文档)。这是它的完成方式:
col.find({...,
'_id' : {'$lt' : {'$oid' : '50314b8e9bcf000000000000'}}
})
finds documents created earlier than the one that's given by oid. Used together with natural sorting and limiting you can utilize BSON _ids to create Twitter-like API queries (give me the last OID you have and I'll provide twenty more)
查找早于 oid 给出的文档创建的文档。与自然排序和限制一起使用,您可以利用 BSON _ids 创建类似 Twitter 的 API 查询(给我您拥有的最后一个 OID,我将提供二十多个)
回答by es2
In python you can do this:
在python中你可以这样做:
>>> from bson.objectid import ObjectId
>>> gen_time = datetime.datetime(2010, 1, 1)
>>> dummy_id = ObjectId.from_datetime(gen_time)
>>> result = collection.find({"_id": {"$lt": dummy_id}})
I think, ObjectId.from_datetime() - its a useful method of standard bson lib Maybe other language bindings have alternative builtin function. Source: http://api.mongodb.org/python/current/api/bson/objectid.html
我认为, ObjectId.from_datetime() - 它是标准 bson lib 的一种有用方法,也许其他语言绑定有替代的内置函数。来源:http: //api.mongodb.org/python/current/api/bson/objectid.html