How to get a component like minute from ISODate stored in MongoCollection?
Asked
Active
Viewed 1.6k times
11
McGarnagle
- 101,349
- 31
- 229
- 260
Saint
- 5,397
- 22
- 63
- 107
1 Answers
23
Since you don't specify a language, I'm going to assume you mean JavaScript, as in the shell.
One of the nice features of the shell is it has tab completion. So you can do something like this:
> db.test.insert({x:new Date()});
> var doc = db.test.findOne();
> doc
{
"_id" : ObjectId("4fa131851932655dc45027a9"),
"x" : ISODate("2012-05-02T13:07:17.012Z")
}
> doc.x
ISODate("2012-05-02T13:07:17.012Z")
> doc.x.<TAB><TAB>
doc.x.constructor doc.x.getSeconds( doc.x.getUTCMinutes( doc.x.setHours( doc.x.setUTCHours( doc.x.toLocaleDateString(
doc.x.getDate( doc.x.getTime( doc.x.getUTCMonth( doc.x.setMilliseconds( doc.x.setUTCMilliseconds( doc.x.toLocaleString(
doc.x.getDay( doc.x.getTimezoneOffset( doc.x.getUTCSeconds( doc.x.setMinutes( doc.x.setUTCMinutes( doc.x.toLocaleTimeString(
doc.x.getFullYear( doc.x.getUTCDate( doc.x.getYear( doc.x.setMonth( doc.x.setUTCMonth( doc.x.toString(
doc.x.getHours( doc.x.getUTCDay( doc.x.hasOwnProperty( doc.x.setSeconds( doc.x.setUTCSeconds( doc.x.toTimeString(
doc.x.getMilliseconds( doc.x.getUTCFullYear( doc.x.propertyIsEnumerable( doc.x.setTime( doc.x.setYear( doc.x.toUTCString(
doc.x.getMinutes( doc.x.getUTCHours( doc.x.setDate( doc.x.setUTCDate( doc.x.toDateString( doc.x.tojson(
doc.x.getMonth( doc.x.getUTCMilliseconds( doc.x.setFullYear( doc.x.setUTCFullYear( doc.x.toGMTString( doc.x.valueOf(
What you want is probably:
> doc.x.getSeconds();
17
> doc.x.getMinutes();
7
> doc.x.getHours();
9
> doc.x.getDate();
2
> doc.x.getMonth();
4
> doc.x.getFullYear();
2012
Eve Freeman
- 32,467
- 4
- 86
- 101
-
1why ´doc.x.getMonth();´ returns 4 instead of 5. – Moj Feb 10 '14 at 13:20
-
1Probably because MongoDB uses zero based indexes (starts counting at 0 instead of 1) – DJ van Wyk Aug 12 '14 at 10:37
-
2getMonth() is javascript function. It starts from 0. See this http://www.w3schools.com/jsref/jsref_getmonth.asp – okisinch Feb 12 '15 at 09:28