0

I am inserting Date Object from Java to Mongodb, mongo ISODate(...) stores Date with Time, my requirement is to Store both in different field.

is this possible ?

note : don't want solution that stores milliseconds instead of date. want to store only date like "2013-06-19" and time like "00:00:00"

if i use String format for this than hard to perform date operation.

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
  • DB db = mongo.getDB("demo"); DBCollection dbCollection = db.getCollection("testcoll"); Date date = new Date(); dbCollection.save(new BasicDBObject("date", date)); – Yogesh Prajapati Jun 19 '13 at 06:18
  • 2
    No, MongoDb doesn't have a "date only" or "time only" field. http://docs.mongodb.org/manual/reference/glossary/#term-bson Consider just storing a date value as a `Date` instance without the time and the time as an integer. – WiredPrairie Jun 19 '13 at 10:55

1 Answers1

2

You can convert the Date object to unix timestamp as timestamp, then store timestamp / 86400 to a field and timestamp % 86400 to another field.

lqs
  • 1,434
  • 11
  • 20
  • after inserting , Mongo Object looks like {"date": ISODate("2013-06-09"),"Time": "00:02:00"} ? – Yogesh Prajapati Jun 19 '13 at 06:56
  • @yogeshprajapati convert the date to 1370707320, calc the date 1370707320 / 86400 = 57720 and the time 1370707320 % 86400 = 57720. and then store them into mongodb: `{"date": 15864, "time": 57720}` – lqs Jun 19 '13 at 07:05
  • i know but i want same as given above, Note is written in my question. – Yogesh Prajapati Jun 19 '13 at 08:38