MongoDB – Store date and optional time as a field

mongodb

I'm developing a web service using MEAN stack + Mongoose, and one of the things I need to implement is storing a date as an object's field. But the thing is, the time should be optional, e.g. the user should be able to save an object filling in only the date without time.

How can I achieve this result? And how can I search on this field later?

Best Answer

You can always insert object with type "date" (type 9), no special codes needed. As you can always insert it as "string" or ISODate.