Mongodb日期存储为历元
因此,出于某种原因,Mongodb将日期存储为EpochMongodb日期存储为历元,mongodb,Mongodb,因此,出于某种原因,Mongodb将日期存储为Epoch1593236606706 但是现在我需要查询它。因为我需要从这些中得到月份和年份 我想知道您是如何要求mongodb将历元转换为日期的 我正在尝试以下方法 { $convert: { input: "history.starttime", to: "date" } } 我也试过了 $addField:{ convertedDate: { $toDate: "$history
1593236606706
但是现在我需要查询它。因为我需要从这些中得到月份和年份
我想知道您是如何要求mongodb将历元转换为日期的
我正在尝试以下方法
{ $convert: { input: "history.starttime", to: "date" } }
我也试过了
$addField:{
convertedDate: { $toDate: "$history.starttime" }
}
但是不,我想是因为我不能调用子数组
我也试过这个
{
history:{
convertedDate: { $toDate:"$history[starttime]" }
}
}
但这只是把它放在正确的子数组中,仍然无法从该子数组中获取开始日期
db.collection.aggregate([
{
"$unwind": "$history"
},
{
"$project": {
"d": {
$convert: {
input: "$history.key",
to: "date"
}
}
}
}
])
您需要将它作为$project
的一部分放在展开之后,因为它是一个数组
如果希望使用$group
将原始结构重新组合到project之后
您需要将它作为$project
的一部分放在展开之后,因为它是一个数组
如果希望使用$group
将原始结构带入project,则可以在project之后对其进行重新分组示例:
首先需要$unwind数组,然后进行转换。在末尾重新组合它示例:
首先需要$unwind数组,然后进行转换。在结尾重新分组有人问我{path:path,includearayindex:'string',preserveNullAndEmptyArrays:boolean}你能把你的文档粘贴成文本吗?有人问我{path:path,includearayindex:'string',preserveNullAndEmptyArrays:boolean}你能把文档粘贴成文本吗?