Javascript 从 mongodb id 获取时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6452021/
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
Getting timestamp from mongodb id
提问by Harry
How do I get the timestamp from the MongoDB id?
如何从 MongoDB id 获取时间戳?
回答by Kolja
The timestamp is contained in the first 4 bytes of a mongoDB id (see: http://www.mongodb.org/display/DOCS/Object+IDs).
时间戳包含在 mongoDB id 的前 4 个字节中(请参阅:http: //www.mongodb.org/display/DOCS/Object+IDs)。
So your timestamp is:
所以你的时间戳是:
timestamp = _id.toString().substring(0,8)
and
和
date = new Date( parseInt( timestamp, 16 ) * 1000 )
回答by Chris Lynch
As of Mongo 2.2, this has changed (see: http://docs.mongodb.org/manual/core/object-id/)
从 Mongo 2.2 开始,这已经改变了(参见:http: //docs.mongodb.org/manual/core/object-id/)
You can do this all in one step inside of the mongo shell:
您可以在 mongo shell 中一步完成这一切:
document._id.getTimestamp();
This will return a Date object.
这将返回一个 Date 对象。
回答by Eric Leschinski
Get the timestamp from a mongoDB collection item, with walkthrough:
通过演练从 mongoDB 集合项中获取时间戳:
The timestamp is buried deep within the bowels of the mongodb object.
时间戳深埋在 mongodb 对象的内部。
Login to mongodb shell
登录mongodb shell
ubuntu@ip-10-0-1-223:~$ mongo 10.0.1.223
MongoDB shell version: 2.4.9
connecting to: 10.0.1.223/test
Create your database by inserting items
通过插入项目创建数据库
> db.penguins.insert({"penguin": "skipper"})
> db.penguins.insert({"penguin": "kowalski"})
>
Check if it is there:
检查它是否存在:
> show dbs
local 0.078125GB
penguins 0.203125GB
Lets make that database the one we are on now
让该数据库成为我们现在使用的数据库
> use penguins
switched to db penguins
Get yourself an ISODate:
给自己一个 ISODate:
> ISODate("2013-03-01")
ISODate("2013-03-01T00:00:00Z")
Print some json:
打印一些json:
> printjson({"foo":"bar"})
{ "foo" : "bar" }
Get the rows back:
取回行:
> db.penguins.find()
{ "_id" : ObjectId("5498da1bf83a61f58ef6c6d5"), "penguin" : "skipper" }
{ "_id" : ObjectId("5498da28f83a61f58ef6c6d6"), "penguin" : "kowalski" }
We only want to inspect one row
我们只想检查一行
> db.penguins.findOne()
{ "_id" : ObjectId("5498da1bf83a61f58ef6c6d5"), "penguin" : "skipper" }
Get the _id of that row:
获取该行的 _id:
> db.penguins.findOne()._id
ObjectId("5498da1bf83a61f58ef6c6d5")
Get the timestamp from the _id object:
从 _id 对象获取时间戳:
> db.penguins.findOne()._id.getTimestamp()
ISODate("2014-12-23T02:57:31Z")
Get the timestamp of the last added record:
获取最后添加的记录的时间戳:
> db.penguins.find().sort({_id:-1}).limit(1).forEach(function (doc){ print(doc._id.getTimestamp()) })
Tue Dec 23 2014 03:04:53 GMT+0000 (UTC)
Example loop, print strings:
示例循环,打印字符串:
> db.penguins.find().forEach(function (doc){ print("hi") })
hi
hi
Example loop, same as find(), print the rows
示例循环,与 find() 相同,打印行
> db.penguins.find().forEach(function (doc){ printjson(doc) })
{ "_id" : ObjectId("5498dbc9f83a61f58ef6c6d7"), "penguin" : "skipper" }
{ "_id" : ObjectId("5498dbd5f83a61f58ef6c6d8"), "penguin" : "kowalski" }
Loop, get the system date:
循环,获取系统日期:
> db.penguins.find().forEach(function (doc){ doc["timestamp_field"] = new Date(); printjson(doc); })
{
"_id" : ObjectId("5498dbc9f83a61f58ef6c6d7"),
"penguin" : "skipper",
"timestamp_field" : ISODate("2014-12-23T03:15:56.257Z")
}
{
"_id" : ObjectId("5498dbd5f83a61f58ef6c6d8"),
"penguin" : "kowalski",
"timestamp_field" : ISODate("2014-12-23T03:15:56.258Z")
}
Loop, get the date of each row:
循环,获取每一行的日期:
> db.penguins.find().forEach(function (doc){ doc["timestamp_field"] = doc._id.getTimestamp(); printjson(doc); })
{
"_id" : ObjectId("5498dbc9f83a61f58ef6c6d7"),
"penguin" : "skipper",
"timestamp_field" : ISODate("2014-12-23T03:04:41Z")
}
{
"_id" : ObjectId("5498dbd5f83a61f58ef6c6d8"),
"penguin" : "kowalski",
"timestamp_field" : ISODate("2014-12-23T03:04:53Z")
}
Filter down to just the dates
过滤到仅日期
> db.penguins.find().forEach(function (doc){ doc["timestamp_field"] = doc._id.getTimestamp(); printjson(doc["timestamp_field"]); })
ISODate("2014-12-23T03:04:41Z")
ISODate("2014-12-23T03:04:53Z")
Filterdown further for just the strings:
仅针对字符串进一步过滤:
> db.penguins.find().forEach(function (doc){ doc["timestamp_field"] = doc._id.getTimestamp(); print(doc["timestamp_field"]) })
Tue Dec 23 2014 03:04:41 GMT+0000 (UTC)
Tue Dec 23 2014 03:04:53 GMT+0000 (UTC)
Print a bare date, get its type, assign a date:
打印一个空日期,获取它的类型,分配一个日期:
> print(new Date())
Tue Dec 23 2014 03:30:49 GMT+0000 (UTC)
> typeof new Date()
object
> new Date("11/21/2012");
ISODate("2012-11-21T00:00:00Z")
Convert instance of date to yyyy-MM-dd
将日期实例转换为 yyyy-MM-dd
> print(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate())
2014-1-1
get it in yyyy-MM-dd format for each row:
以 yyyy-MM-dd 格式获取每一行:
> db.penguins.find().forEach(function (doc){ d = doc._id.getTimestamp(); print(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()) })
2014-12-23
2014-12-23
the toLocaleDateString is briefer:
toLocaleDateString 更简短:
> db.penguins.find().forEach(function (doc){ d = doc._id.getTimestamp(); print(d.toLocaleDateString()) })
Tuesday, December 23, 2014
Tuesday, December 23, 2014
Get each row in yyyy-MM-dd HH:mm:ss format:
以 yyyy-MM-dd HH:mm:ss 格式获取每一行:
> db.penguins.find().forEach(function (doc){ d = doc._id.getTimestamp(); print(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()) })
2014-12-23 3:4:41
2014-12-23 3:4:53
Get the date of the last added row:
获取最后添加行的日期:
> db.penguins.find().sort({_id:-1}).limit(1).forEach(function (doc){ print(doc._id.getTimestamp()) })
Tue Dec 23 2014 03:04:53 GMT+0000 (UTC)
Drop the database when you are done:
完成后删除数据库:
> use penguins
switched to db penguins
> db.dropDatabase()
{ "dropped" : "penguins", "ok" : 1 }
Make sure it's gone:
确保它消失了:
> show dbs
local 0.078125GB
test (empty)
Now your MongoDB is webscale.
现在您的 MongoDB 是网络规模的。
回答by webmaster
Here is a quick php function for you all ;)
这是一个快速的 php 函数给你们;)
public static function makeDate($mongoId) {
$timestamp = intval(substr($mongoId, 0, 8), 16);
$datum = (new DateTime())->setTimestamp($timestamp);
return $datum->format('d/m/Y');
}
回答by poke19962008
In the server sidemake _id
of MongoDB ObjectId
在服务器端制作_id
MongoDB ObjectId
date = new Date( parseInt( _id.toString().substring(0,8), 16 ) * 1000 )
date = new Date( parseInt( _id.toString().substring(0,8), 16 ) * 1000 )
And on the client sideuse
并在客户端使用
var dateFromObjectId = function (objectId) {
return new Date(parseInt(objectId.substring(0, 8), 16) * 1000);
};
回答by Alok Deshwal
from official documentation:
来自官方文档:
ObjectId('mongodbIdGoesHere').getTimestamp();