1

After logging into MongoDB server with shell, I try the following commands

rs1:PRIMARY> x = Date()
Thu Dec 12 2013 11:25:58 GMT+0800 (CST)
rs1:PRIMARY> x -1
NaN
rs1:PRIMARY> print(x)
Thu Dec 12 2013 11:25:58 GMT+0800 (CST)
rs1:PRIMARY> x - 1000
NaN

In MongoDB doc, http://docs.mongodb.org/manual/reference/bson-types/ the Date type is a 64bit integer which represents the mill-seconds since epoch. I want to show its value, but always get one string.

Dean Chen
  • 3,800
  • 8
  • 45
  • 70
  • possible duplicate of [Date vs new Date in JavaScript](http://stackoverflow.com/questions/9584719/date-vs-new-date-in-javascript) – WiredPrairie Dec 12 '13 at 12:13

2 Answers2

2

Its a Javascript Date object. getTime() returns the millisecond value as an integer

dmReplSet:PRIMARY> var now = new Date();
dmReplSet:PRIMARY> now.getTime()
1386820859427
dmReplSet:PRIMARY> 
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
1

Use the new operator.

$ mongo
MongoDB shell version: 2.4.5
connecting to: test
> var x = new Date()
> x - 1
1386820087199
> print(x)
Thu Dec 12 2013 01:48:07 GMT-0200 (BRST)
> x - 1000
1386820086200
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62