开发者

Emit a DateTime in the Map function of MongoDb

开发者 https://www.devze.com 2023-02-09 14:53 出处:网络
My map function looks like this: map = function() { day = Date.UTC(this.TimeStamp.getFullYear(), this.TimeStamp.getMon开发者_如何学Cth(), this.TimeStamp.getDate());

My map function looks like this:

map = function()
{
   day = Date.UTC(this.TimeStamp.getFullYear(), this.TimeStamp.getMon开发者_如何学Cth(), this.TimeStamp.getDate());
   emit({day : day, store_id : this.Store_Id}, {count : 1});
}

TimeStamp is stored as date in the database, like this:

{ "TimeStamp" : "Mon Mar 01 2010 11:58:09 GMT+0000 (BST)", ...}

I need the "day" in the result collection to be stored as a date type as well, but it's stored as long (Epoch ticks) like this:

{ "_id" : { "day" : 1265414400000, "store_id" : 10}, "value" : { "count" : 7 } }

I tried changing the emit to something like this but didn't help:

emit({day : {"$date" : day},...)

Any ideas as to how to do that?


Date.utc is going to return miliseconds from epoch. So when you put your data back into the DB, you can use for example:

 new Date(dateAsLong)

and it will be stored as the BSON date format.

earlier than mongo 1.7 it will show up in your hash as:

 "Mon Mar 01 2010 11:58:09 GMT+0000 (BST)"

1.7+ it will appear as:

 ISODate("2010-03-01T11:58:09Z")
0

精彩评论

暂无评论...
验证码 换一张
取 消