關於javascript:從mongodb id獲取時間戳

如何從MongoDB ID獲取時間戳?

時間戳記包含在mongoDB ID的前4個字節中(請參閱:http://www.mongodb.org/display/DOCS/Object+ID)。

因此,您的時間戳是:

1
timestamp = _id.toString().substring(0,8)

1
date new Date( parseInt( timestamp16 1000 )

從Mongo 2.2開始,這種情況已經改變(請參閱:http://docs.mongodb.org/manual/core/object-id/)

您可以在mongo shell內一步完成全部操作:

1
document._id.getTimestamp();

這將返回一個Date對象。

通過演練從mongoDB集合項獲取時間戳:

 

時間戳被埋在mongodb對象的腸子深處。跟隨並保持霜凍。

登錄到mongodb shell

1
2
3
ubuntu@ip-10-0-1-223:~$ mongo 10.0.1.223
MongoDB shell version: 2.4.9
connecting to: 10.0.1.223/test

通過插入項目創建數據庫

1
2
3
> db.penguins.insert({"penguin":"skipper"})
> db.penguins.insert({"penguin":"kowalski"})
>

檢查是否存在:

1
2
3
> show dbs
local      0.078125GB
penguins   0.203125GB

讓該數據庫成爲我們現在使用的數據庫

1
2
> use penguins
switched to db penguins

獲取ISODate:

1
2
> ISODate("2013-03-01")
ISODate("2013-03-01T00:00:00Z")

打印一些json:

1
2
> printjson({"foo":"bar"})
{"foo" :"bar" }

獲取行:

1
2
3
> db.penguins.find()
{"_id" : ObjectId("5498da1bf83a61f58ef6c6d5"),"penguin" :"skipper" }
{"_id" : ObjectId("5498da28f83a61f58ef6c6d6"),"penguin" :"kowalski" }

我們只想檢查一行

1
2
> db.penguins.findOne()
{"_id" : ObjectId("5498da1bf83a61f58ef6c6d5"),"penguin" :"skipper" }

獲取該行的_id:

1
2
> db.penguins.findOne()._id
ObjectId("5498da1bf83a61f58ef6c6d5")

從_id對象獲取時間戳:

1
2
> db.penguins.findOne()._id.getTimestamp()
ISODate("2014-12-23T02:57:31Z")

獲取最後添加的記錄的時間戳:

1
2
> 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)

示例循環,打印字符串:

1
2
3
> db.penguins.find().forEach(function (doc){ print("hi"})
hi
hi

示例循環與find()一樣,打印行

1
2
3
> db.penguins.find().forEach(function (doc){ printjson(doc})
{"_id" : ObjectId("5498dbc9f83a61f58ef6c6d7"),"penguin" :"skipper" }
{"_id" : ObjectId("5498dbd5f83a61f58ef6c6d8"),"penguin" :"kowalski" }

循環,獲取系統日期:

1
2
3
4
5
6
7
8
9
10
11
> 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")
}

循環,獲取每一行的日期:

1
2
3
4
5
6
7
8
9
10
11
> 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")
}

僅過濾日期

1
2
3
> 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")

僅對字符串進一步過濾:

1
2
3
> 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)

打印空日期,獲取其類型,分配日期:

1
2
3
4
5
6
> 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")

將日期實例轉換爲yyyy-MM-dd

1
2
> print(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate())
2014-1-1

每行以yyyy-MM-dd格式獲取:

1
2
3
> db.penguins.find().forEach(function (doc){ d = doc._id.getTimestamp(); print(d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()})
2014-12-23
2014-12-23

toLocaleDateString比較簡短:

1
2
3
> db.penguins.find().forEach(function (doc){ d = doc._id.getTimestamp(); print(d.toLocaleDateString()})
Tuesday, December 232014
Tuesday, December 232014

以yyyy-MM-dd HH:mm:ss格式獲取每一行:

1
2
3
> 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

獲取最後添加的行的日期:

1
2
> 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)

完成後刪除數據庫:

1
2
3
4
> use penguins
switched to db penguins
> db.dropDatabase()
{"dropped" :"penguins","ok" }

確保它不存在:

1
2
3
> show dbs
local   0.078125GB
test    (empty)

現在您的MongoDB是webscale。

 


這是適合大家的快速php函數;)

1
2
3
4
5
6
7
8
public static function makeDate($mongoId{

    $timestamp = intval(substr($mongoId08)16);

    $datum (new DateTime())->setTimestamp($timestamp);

    return $datum->format('d/m/Y');
}

在服務器端,使MongoDB ObjectId爲_id

date = new Date( parseInt( _id.toString().substring(0,8), 16 ) * 1000 )

並在客戶端使用

1
2
3
var dateFromObjectId function (objectId{
return new Date(parseInt(objectId.substring(08)161000);
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章