【发布时间】:2021-08-14 19:34:54
【问题描述】:
我从 MongoDB Api 收到一个带有一些预期结果的数组:
[
{
"_id": {
"$oid": "6108f221faf75fa7899e476c"
},
"id": {
"$numberInt": "7"
},
"item1": "XX",
"item2": "YY",
"item3": "ZZ",
"itemdate1": {
"$date": {
"$numberLong": "1627976225150"
}
},
"itemdate2": {
"$date": {
"$numberLong": "1627976225150"
}
},
"itemdate5": {
"$date": {
"$numberLong": "1627976225150"
}
},
"itemdate4": {
"$date": {
"$numberLong": "1627976225150"
}
},
},
{
"_id": {
"$oid": "6108f2219d0f3c7c8b188607"
},
"id": {
"$numberInt": "11"
},
"item1": "AA",
"item2": "BB",
"item3": "CC",
"itemdate1": {
"$date": {
"$numberLong": "1627976225150"
}
},
"itemdate2": {
"$date": {
"$numberLong": "1627976225150"
}
},
"itemdat3": {
"$date": {
"$numberLong": "1627976225150"
}
},
"itemdate4": {
"$date": {
"$numberLong": "1627976225150"
}
},
}
]
这只是一个例子,我可能会收到其中的 20 或 30 个,而且还有更多的项目。
我找不到一种方法来遍历日期对象并修改这些日期对象以删除 $date $ numberlong 字段以在最后得到相同的数组,但只有日期(如下所示):
"itemdate1" : "1627976225150"
而不是
"itemdate1": {
"$date": {
"$numberLong": "1627976225150"
}
},
提前感谢您的帮助
【问题讨论】:
-
我假设您没有使用应该为您进行日期转换的MongoDB Node Driver。你不是有理由吗?如果是这样,我可以编写一些递归代码来进行转换...
-
嗨 @edemaine 我使用 mongodb Realm 作为 API,我直接收到这类请求。
-
docs.mongodb.com/realm/sdk/node/data-types/field-types 表示 Realm SDK 应该支持日期。您是否在架构中使用了
"date"? -
不,我没有完全创建架构。我会那样做的。
-
@edemaine,我检查了,但这不适用于我,因为我正在使用 MongoDB Realm 的第三方生成 api。
标签: javascript arrays date object