因此,由于某种原因,Mongodb将数据存储为Epoch 1593236606706
但是,现在我需要查询它。因为我需要每个月和一年的时间。
我想知道你是如何要求mongodb将时代转换成现在的?
我正在尝试以下几种方法
{ $convert: { input: "history.starttime", to: "date" } }

我也试过
$addField:{
convertedDate: { $toDate: "$history.starttime" }
}但是不,我想是因为我不能调用子数组?
也试过这个
{
history:{
convertedDate: { $toDate:"$history[starttime]" }
}
}但这只是将其放在正确的子数组中--仍然无法从子数组中获取开始日期。
发布于 2020-08-25 03:13:09
db.collection.aggregate([
{
"$unwind": "$history"
},
{
"$project": {
"d": {
$convert: {
input: "$history.key",
to: "date"
}
}
}
}
])您需要在$project之后将它作为unwinding的一部分,因为它是一个数组。
如果您想要使用$group带来原始结构,您可以在项目结束后重新组合它。
发布于 2020-08-25 03:13:14
示例:https://mongoplayground.net/p/sNG9Wh2HeI4
您需要先对数组进行$unwind,然后进行转换。在最后重组它
https://stackoverflow.com/questions/63571437
复制相似问题