Re: Convert NumberLong to Date operations in Aggregation Framework

1,746 views
Skip to first unread message

Ed Bragg

unread,
Nov 30, 2012, 5:50:11 PM11/30/12
to mongod...@googlegroups.com
Does anybody on the MongoDB Aggregate Framework team have any idea on this?

On Tuesday, November 27, 2012 8:24:29 PM UTC-5, Ed Bragg wrote:
db.activity.find()
{  "_id" : ObjectId("50ad5178e93ad575ae76cfd4"),"count" : "1", "lastUsed" : NumberLong("1351589819909"), "userId" : "50ad4f80e93ad575ae76b9af" }

db.activity.aggregate({$project:{_id:0,lastUsed:1,test:{$year: new Date("$lastUsed")},test2:{$year:new Date(NumberLong("1351589819909"))}}});
results in:
 {
         "lastUsed" : NumberLong("1351589819909"),
         "test" : 1970,
         "test2" : 2012
 }

I expected "test":2012, but keeps coming up as 1970. 

I want to be able to use the aggregation framework to set some Date operations ($year, $month, $dayOfYear, etc) against a set NumberLong value in my document. 

Is this even possible?  

Regards,
Ed Bragg

Eliot Horowitz

unread,
Nov 30, 2012, 10:53:50 PM11/30/12
to mongod...@googlegroups.com
There isn't a function to go from milliseconds to Date yet.
The way you wrote it, "new Date( ... )" is being executed in the shell
before being sent to the server.

You should open a request @ http://jira.mongodb.org/ but for now might
have to use map/reduce or store Date.
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
Reply all
Reply to author
Forward
0 new messages