在猫鼬中存储一天中时间的最佳方法 [英] Best way to store time of day in Mongoose

查看:44
本文介绍了在猫鼬中存储一天中时间的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mongoose中创建一个架构,并且试图找出存储代表一天中时间(即3:30)的字段的最佳方法,它不需要是完全限定的时间戳,因为日期是无关紧要的.

解决方案

我建议将其存储为午夜以来的秒数(作为Number)或以24小时格式存储的填充数字String. /p>

例如,3:30PM:

  1. 秒(存储为数字):55800
  2. 字符串:"1530"(始终必须为24小时格式,且前导数字必须具有相同的位数,因此8:30AM将为"0830"

两者都可以按范围进行排序,索引和查询.两者占用大约相同的字节数.由于两者都不是非常人性化的可读性,因此您可能需要以任何一种方式对其进行格式化以进行显示.哪种方法更适合您使用完全取决于您.

I'm creating a schema in Mongoose, and I'm trying to figure out the best way to store a field representing the time of day ie 3:30, it does not need to be a fully qualified timestamp because the date is irrelevant.

解决方案

I'd suggest storing it either as seconds since midnight (as a Number) or as a padded numeric String stored in 24 hour format.

For example, 3:30PM:

  1. Seconds (stored as a number): 55800
  2. String: "1530" (always must be 24 hour format with a leading numeric digit to have the same number of places, so 8:30AM would be "0830"

Both can be sorted, indexed, queried by range. Both take approximately the same number of bytes. Since neither is very human friendly readable, you'd probably need to format them either way for display. It's really up to you which one would work better for your use.

这篇关于在猫鼬中存储一天中时间的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆