Sau*_*abh 5 mongoose mongodb node.js
如何使用 mongoose schmea 仅保存日期部分。目前我的模型是这样的:
myDate: { type: Date}
"2016-02-27T00:00:00.000Z"即使我只通过:“2016-02-27”也能保存日期。
我在用着:
dateOfBirth: { type: Object }
Run Code Online (Sandbox Code Playgroud)
并保存如下:
dateOfBirth: {year: 1988, month: 0, day: 8}
Run Code Online (Sandbox Code Playgroud)
这给了我:
搜索能力year,month
制作日期对象的能力:
const dateOfBirth = new Date(
user.dateOfBirth.year,
user.dateOfBirth.month,
user.dateOfBirth.day
);
Run Code Online (Sandbox Code Playgroud)
这将避免时区转换,因为日期对象将在设备环境中创建
UPD
PS我不是在写优点和缺点,因为我的答案是关于单独存储日期的替代方法,并且使用这种方法时应该是显而易见的。
在下面的评论中与SergiiStotskyi聊天后,我决定提出优点和缺点,以警告开发人员选择最佳解决方案。
请记住,这种方法几乎没有什么好处。
优点(我只能找到两个):
缺点:
| 归档时间: |
|
| 查看次数: |
4870 次 |
| 最近记录: |