您可以在下面使用
Aggregation
db.documents.aggregate([{
$addFields: {
year: { $toString: {
$trunc: { $divide: [{ $toInt: '$date' }, 10000] } }
},
month: {
$toString: {
$trunc: { $divide: [{ $mod: [{ $toInt: '$date' }, 10000] }, 100] }
}
},
day: {
$toString: {
$trunc: { $mod: [{ $toInt: '$date' }, 100] }
}
}
}
},
{
$project: {
_id: 0,
date: {
$toDate: {
$cond: [
{ $ne: ['$year', '0'] },
{ $concat: ['$year', '-', '$month', '-', '$day'] },
new Date("")
]
}
}
}
}
])
如果你想转换
日期
“YYYYMMDD”字符串转换为
年
(“YYYY”)
月
(“mm”)
白天
“dd”,那么我们可以让它更简单