如何在MongoDB中存储时间?作为一个字符串?给任意年/月/日?

Che*_*aks 12 mongodb

例如"23:55:00"

我正在将公共汽车时刻表数据库从Postgres转换为Mongo.
Postgres有一个time without timezone我用来存储停止时间的数据类型,而我很难弄清楚如何迁移它.

将它转换为javascript Date对象给它任意年/月/日似乎没有多大意义,但我确实计划在数据被解析后用时间进行一些简单的计算,以显示像'下一次停止时间',所以如果我将它存储为字符串,我仍然必须将其转换为最终进行比较的东西

我是MongoDB的新手,非常感谢任何建议

ass*_*ias 25

您可以将时间存储为数字(自00:00:00以来的秒数).它会自然地排序,并且在需要时很容易从/转换为hh:mm:ss格式.

//from 23:55:00 to the stored time
storedTime = hours * 3600 + minutes * 60 + seconds

//from the stored time to 23:55:00
hours = storedTime / 3600  // needs to be an integer division
leaves = storedTime - hours * 3600
minutes = leaves / 60
seconds = leaves - 60 * minutes
Run Code Online (Sandbox Code Playgroud)