Show
Nitesh Kumawatunread, 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 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 Kamskyunread, 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 Hooiunread, 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:
Essentially, you have two approaches to storing date-only fields:
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 Kumawatunread, 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 Kamskyunread, 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 Introduction to MongoDB Date QueryMongoDB 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?
Various Date Format in MongoDBGiven below are the date format, and each returns a resulting ISODate instance:
1. new Date (<milliseconds>)
Example: Code:
Output: 2. new Date (<YYYY-mm-dd THH:MM:ss>)
Example: Code:
Output: 3. new Date (<YYYY-mm-dd>)
Example: Code:
Output: 4. new Date(“<YYYY-mm-ddTHH:MM:ssZ>”)
Example: Code:
Output: Examples of MongoDB Date QueryGiven below are the examples of MongoDB Date Query: Example #1To 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:
Output: Example #2To 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:
Output: Example #3Use Date () method in MongoDB. In below example, we have used the date function. Code:
Output: Example #4Use a new Date () method in MongoDB. In below example, we have used the date function. Code:
Output:
ConclusionDate 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. Recommended ArticlesThis 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 –
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”.
|