mongodb - 将技术日期格式化为 MongoDB 中用户可以理解的格式
问题描述
{
time : "2016-04-30T21"
}
需要格式化用户可以理解的日期。
预期输出:
{
time: 21h 30.04.2016
}
解决方案
$dateFromString
to convert date to date type from string$dateToString
convert above converted date to specific format
db.collection.aggregate([
{
$project: {
time: {
"$dateToString": {
"date": {
"$dateFromString": {
"dateString": "$time",
"format": "%Y-%m-%dT%H"
}
},
"format": "%Hh %d.%m.%Y"
}
}
}
}
])
NOTE:
- It would be easy and fast this conversation in the client-side language (node/js), I would not recommend this date format customization approach in the query because why we add more process time in the query instead we can achieve in client-side in quickly.
- I would suggest you store this date to date type instead of string type because it will really help to match query and conversation to specific timezone format without any extra operators.
推荐阅读
- sql - 在 Rails 中从 SQL 中整理数据
- angular - 为什么简单的 Angular App 映像不能使用 Docker 在本地运行?
- postgresql - 从 wildfly 设置 Postgres SSL 连接
- ios - 导航离开并返回后 ViewController 为空白
- c# - 如何指定 createfilew 函数获取未缓存的结果?
- python - 如何在循环计算之后保存或覆盖数据框中的列值
- android - 这种隐式广播与通过显式广播进行相同操作的其他方式有什么区别?
- python - 矛盾的线条——对象还是阶级?
- html - 我如何使用 content.match(谷歌应用脚本)读取带有空白行的 html 源代码
- html - 如何使用按钮在ie8中添加链接?