MongoDB shell version: 2.0.4
Want to insert date as 'date datatype' from mongo command line.
db.my_date_collection.insert(
{"user_id" : 91,
"event_timestamp" :new Date("09-AUG-12 05.30.28.402000 AM")
});
Above query add record but change date from "09-AUG-12 05.30.28.402000 AM" to "1970-01-01T00:00:00Z"
> db.my_date_collection.findOne()
{
"_id" : ObjectId("54168ddc289a635d725d86fb"),
"user_id" : 91,
"event_timestamp" : ISODate("1970-01-01T00:00:00Z")
}
Plus it also didn't save 'date data' as date 'datatype'
typeof db.my_date_collection.findOne().event_timestamp;
object
Can someone help me insert data as date type from mongo prompt?
Additionally can some one tell me how to insert the 'date as date datatype type' from a tsv file? as i got the same issue in loading tsv from mongoimport.
Thanks
The date is not in a valid format to be parsed by the date constructor. Try the equivalent value in a valid format (I'm assuming UTC time since you didn't include a timezone):
"09-AUG-12 05.30.28.402000 AM" => "2012-08-09T05:30:28.402"
> var my_date = ISODate("2012-08-09T05:30:28.402")
> db.my_dates.insert({ "x" : my_date })
> var doc = db.my_dates.findOne()
> doc.x instanceof Date
true
The return value is of Date type, but Javascript isn't object oriented so the meaning of that and how to determine if something is of Date type are different than you are expecting, apparently. Use instanceof to check is something is of a certain type or "inherits" from that type.