[Answered ]-How to save only date in Mongodb with Django DateField

2đź‘Ť

âś…

This is doing exactly what you want it to do even though you don’t think that is what it is doing. To explain, this is what you are seeing in the mongo shell:

 ISODate("2014-02-24T00:00:00Z") 

But that is not actually the value that is in the field, it is just how the shell represents it. So it’s not a string value like you might think.

Internally the value in the field is a BSON date, which is a specially tagged version of an epoch time value. This is very useful, and what you really want, as it is considered by the server to be a proper date, and can be compared to other date values, have different date operations performed on it, etc.

Also just as your application “just saved this” from supplying a native date object, when this is read back from the collection you will also get a native date object.

What not to do, is convert these to strings in your application, and thus have to convert back when you read the data.

Stick to using the native dates, it’s what you really want.

👤Neil Lunn

Leave a comment