Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get component of Date / ISODate in mongo

How to get a component like minute from ISODate stored in MongoCollection?

like image 627
Saint Avatar asked May 02 '12 12:05

Saint


People also ask

What is ISODate in MongoDB?

ISODate() is a helper function that's built into to MongoDB and wraps the native JavaScript Date object. When you use the ISODate() constructor from the Mongo shell, it actually returns a JavaScript Date object.

How is date stored in MongoDB?

The DATE type in MongoDB can store date and time values as a combined unit. The BSON Date type is a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970).

How to work date query with isodate in MongoDB?

Use $gte operator along with ISODate () to work Date query with ISODate in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −

How do I get the current date in MongoDB?

The Date () method returns the date as a string or a Date object. In MongoDB shell or mongosh, the Date () method returns the current date as a string. The new Date () function returns a Date object with the current date. The ISODate helper is wrapped around the Date object by mongosh, and the ISODate is in UTC (Universal Time).

How to define a specific date using mongosh?

The new Date () function returns a Date object with the current date. The ISODate helper is wrapped around the Date object by mongosh, and the ISODate is in UTC (Universal Time). You can use the new Date () function or the ISODate () method to define a specific date by giving an ISO-8601 date string with a year between '0' and '9999'.

How do I use the isodate helper with Date object?

The ISODate helper is wrapped around the Date object by mongosh, and the ISODate is in UTC (Universal Time). You can use the new Date () function or the ISODate () method to define a specific date by giving an ISO-8601 date string with a year between '0' and '9999'.


1 Answers

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
like image 81
Eve Freeman Avatar answered Oct 04 '22 12:10

Eve Freeman