Is there a date type in mongodb?

Nitesh Kumawat

unread,

Jun 14, 2014, 3:08:44 AM6/14/14

to

I have imported a excel data to mongoDB. There the date got imported as string. I want date string field to be converted or want to add new field which would be of type date. I am not getting how to do it. I look for some SO posts but I am getting below issue:

I tried to do 
var x = new ISODate("1/2/2014");
it is giving shell error invalid iso date at src/mongo/shell/types.js

Can someone help please?

another thing is if I do 

var x = new Date( )

it gives ISO date with timezone.. 

how to work if I only want Date, I am not interested in TimeZone?

> var x = new Date(2010, 1, 1)

> x

ISODate("2010-01-31T18:30:00Z")

I am getting 31st Jan instead of 1st jan... 

really struggeling to solve this.

-Nitesh

Asya Kamsky

unread,

Jun 14, 2014, 10:02:34 PM6/14/14

to mongodb-user

The date type supported in MongoDB will be to the second granularity so you will get ISODate() with 0's filled in past the day as you saw (the reason it's off by 5.5 hours is because it's showing you UTC or Zulu time - I'm guess you're in India time zone?)

If you want it as date your only option is ISODate() - otherwise you can store it as a string that's well ordered and can be used for comparisons (for example "2014/01/14" is one of those formats, "1/2/2014" is *not* one).

Asya

Victor Hooi

unread,

Jun 16, 2014, 3:35:45 AM6/16/14

to

Hi Nitesh,

Everything Asya said is correct.

Regarding your error message, the constructor for ISODate expects a valid ISO8601-format date. For example, the below will work:

> var x = new ISODate("2014-05-05")> x
ISODate("2014-05-05T00:00:00Z")

Essentially, you have two approaches to storing date-only fields:

  1. Use MongoDB ISODate, and have your application code handle ignoring anything past the day field. You will also need to ensure all of your dates are converted to a uniform timezone (e.g. UTC) prior to importing.
  2. Use a string field and store a well-ordered date - e.g. YYYYMMDD.

Further, in Excel, you can control how dates are displayed. Select your cells, then hit Ctrl + 1 (Windows) or Command + 1 (OSX). Then in the Format Cells dialog, select the appropriate date format.

If you can provide more details on the exact steps you are taking to import this data from Excel to MongoDB, we can help you better.

Please let me know if you have any queries about any of the above.

Regards,

Victor

Nitesh Kumawat

unread,

Jun 18, 2014, 3:15:29 AM6/18/14

to

Thanks Victor and Asya.

So what I understood is I need to format the date in excel before importing. I tried a sample in mongo shell as below

> var x = new ISODate("20140202")

> x

ISODate("2014-02-02T00:00:00Z")

means, If I want to work only with dates then I could use proper format date and use ISODate field. because now it does not have time component in this.

can you let me know what is the difference between date and ISODate data type in mongoDb?

Thanks

Nitesh

Asya Kamsky

unread,

Jun 18, 2014, 7:01:25 PM6/18/14

to mongodb-user

Not sure what you mean - there is only one date type (it's called ISODate()).

There is also a type called Timestamp but we haven't been discussing it in this thread...

If you are referring to JS Date() constructor, that will result in the same ISODate() in the database as you saw.

Asya

Is there a date type in mongodb?

Introduction to MongoDB Date Query

MongoDB date query returns the date either as string or date object, date query will return the current date as a string in the mongo shell. MongoDB date will return the current date as a date object, and mongo shell will wrap the date object with isolate helper in MongoDB. We can specify the particular date by passing the date as an ISO-8601 date string, and this string is used with the range as 0 to 9999 to the new date() function in MongoDB.

How does Date Query work in MongoDB?

  • We can use date () command to get a date as a string in a query without the new keyword or MongoDB shell.
  • While creating a new date object, we can specify the date object as follows.
  • The date query in MongoDB will internally store 64-bit integer value representing the number of milliseconds, and UNIX uses it from 1 Jan 1970.
  • MongoDB date object store 64 bit, but it will not store all database versions and operations to support 64 bit.
  • We can use the date as a string, and we can also use the date as method and object in MongoDB.
  • In MongoDB, we can convert the date object into the string into the specified format. There are different operator available for a date to a string in MongoDB.
  • There are two ways available in MongoDB to store date time they are new Date () and new ISODate ().
  • In the first approach, we can use the date object in MongoDB like javascript. The date object or method is the best method to store the date or time in MongoDB.
  • In the second approach, we can use ISODate method to store date or time in MongoDB.
  • MongoDB does not support the date without time zone format. The alternate way to store date without time zone is to use two types of log representation.
  • First is millisecond and second is (<YYYY-mm-dd HH:MM:ss>). These are the ways that are available in MongoDB to store the date without time zone in MongoDB.
  • We have used ISODate function in MongoDB; this is used to build the native javascript function.
  • ISODate in MongoDB provides a convenient way to represent the date in MongoDB.
  • String date format is used to store the string in date format. This is used to store the date as a simple string in a human-readable format.
  • While storing date in string format, it is very easy to display, while storing the date in string format there is no need to process using the same.
  • Also, while storing the date in string format, it is easy to convert it into any other platform or format. But it is tough to determine date format while storing it into the string format.

Various Date Format in MongoDB

Given below are the date format, and each returns a resulting ISODate instance:

  • new Date (<milliseconds>)
  • new Date (<YYYY-mm-dd THH:MM:ss>)
  • new Date (<YYYY-mm-dd>)
  • new Date(“<YYYY-mm-ddTHH:MM:ssZ>”)

1. new Date (<milliseconds>)

  • A millisecond is an integer, and it specifies the number of milliseconds. Milliseconds defines integer value which was used date format in MongoDB.
  • Milliseconds will convert date into standard date format.
  • Below example shows that convert milliseconds into date format. In below example, we have display two examples. In the first example, we have used 1234567890 milliseconds output of this milliseconds is 1970-01-15 and in the second example we have used 0987654321 milliseconds output of this milliseconds is 1970-01-12.

Example:

Code:

new Date(1234567890)

new Date(0987654321)

Output:

Is there a date type in mongodb?

2. new Date (<YYYY-mm-dd THH:MM:ss>)

  • This format defines the year, month and date in full format, it will also show the hour, minute and second in full format.
  • In below example, we have used the date as (“2020-05-15T12:05:45”), and output of using this date is (“2020-05-15T06:35:45Z”).

Example:

Code:

new Date("2020-05-15T12:05:45")

Output:

Is there a date type in mongodb?

3. new Date (<YYYY-mm-dd>)

  • This format is defined as a year, month and date in full format.
  • In below example, we have used the date as (“2020-05-15”), and output of using this date is (“2020-05-15T00:00:00Z”).

Example:

Code:

new Date("2020-05-15")

Output:

Is there a date type in mongodb?

4. new Date(“<YYYY-mm-ddTHH:MM:ssZ>”)

  • This format defines the year, month and date in full format, it also shows the hour, minute and second in full format.
  • In below example, we have used the date as (“2020-05-15T12:05:45Z”), and output of using this date is (“2020-05-15T06:35:45Z”).

Example:

Code:

new Date("2020-05-15T12:05:45Z")

Output:

Is there a date type in mongodb?

Examples of MongoDB Date Query

Given below are the examples of MongoDB Date Query:

Example #1

To store date using new Date () format in MongoDB.

In below example, we have used new Date () object in MongoDB. We have used test collection while using date object in MongoDB.

Code:

db.test.insertOne ({"Id":"1","Date”: new Date()});
db.test.find ()

Output:

Is there a date type in mongodb?

Example #2

To store date using new ISODate () format in MongoDB.

In below example, we have used new ISODate () object in MongoDB. We have used test1 collection while using date object in MongoDB.

Code:

db.test1.insertOne ({"Id":"1","Date":new ISODate()});
db.test1.find ()

Output:

Is there a date type in mongodb?

Example #3

Use Date () method in MongoDB.

In below example, we have used the date function.

Code:

Date ()

Output:

Is there a date type in mongodb?

Example #4

Use a new Date () method in MongoDB.

In below example, we have used the date function.

Code:

new Date ()

Output:

Is there a date type in mongodb?

Conclusion

Date query is used to return the date as query or string. Basically, there are two ways to store date, i.e. date () and ISODate (). String date format is used to store a string into date format while storing date into string format it’s very easy to display.

This is a guide to MongoDB Date Query. Here we discuss MongoDB Date Query’s introduction, how does date query work with various date format in MongoDB and examples. You may also have a look at the following articles to learn more –

  1. MongoDB Data Types
  2. MongoDB Geospatial
  3. MongoDB MapReduce
  4. MongoDB Limit()

What is the date format in MongoDB?

These functions accept the following formats: new Date("<YYYY-mm-dd>") returns the ISODate with the specified date. new Date("<YYYY-mm-ddTHH:MM:ss>") specifies the datetime in the client's local timezone and returns the ISODate with the specified datetime in UTC.

How are dates stored in MongoDB?

Internally, MongoDB can store dates as either Strings or as 64-bit integers. If you intend to do any operations using the MongoDB query or aggregate functions, or if you want to index your data by date, you'll likely want to store your dates as integers.

What are the data types in MongoDB?

The following are some of the most often used data types in MongoDB..
String. One of the most basic and widely used data types is the string. ... .
Integer. Numeric values are stored using the integer data type. ... .
Double. ... .
Boolean. ... .
Array. ... .
Object. ... .
Date. ... .
Timestamp..

Can we store date as string in MongoDB?

You can safely store dates as strings and query on them as long as they are properly formatted for date, i.e., “YYYY-MM-ddTHH:mm:ss”.