0

I am using java api for mongodb to parse the JSON string into Java object and storing it into MongoDB.

My JSON string will have fields that are date like the one given below. "created":"2012-10-17 21:39:06.385987 +0000"

When I try to save the parsed java object into MongoDB it stores the value as String. I would like to store it has a datetime field. Can someone shed some light on this?

Thanks and Regards, Balaji.R

user1757885
  • 1
  • 1
  • 1

1 Answers1

1

Java driver, as other default drivers, maps basic language types to MongoDB types, so to save date to mongo, you just need to save the Date object, not date string. Here's simple sample from MongoDB documentation.

mrówa
  • 5,671
  • 3
  • 27
  • 39
  • Thanks for your response. I would like to know if there is a way to dynamically convert all the values which are in a specific date format in the JSON string to Date object while parsing the JSON string to java object. Thanks and Regards, Balaji.R – user1757885 Oct 19 '12 at 16:40
  • Nothing I know of. But you can read the date json, take the date string value and convert it in java to java Date object (there are many functions concerning date formatting and converting). – mrówa Oct 20 '12 at 12:07