arrays - 转换 mongo 数组
问题描述
我有一个集合,其中包含像
{
"_id": ObjectId("60f561eb0d022a4c614966c1"),
"vehicleId": 288,
"startTime": [
ISODate("2021-06-19T13:00:19Z"),
ISODate("2021-06-19T13:00:40Z")
],
"odo": [0, 1116.0746443123298],
"location": [
{ "type": "Point", "coordinates": [75.759973, 26.977889] },
{ "type": "Point", "coordinates": [75.771209, 26.97858] }
]
}
我想更改位置数组,使文档看起来像
{
"_id": ObjectId("60f561eb0d022a4c614966c1"),
"vehicleId": 288,
"startTime": [
ISODate("2021-06-19T13:00:19Z"),
ISODate("2021-06-19T13:00:40Z")
],
"odo": [0, 1116.0746443123298],
"locations": [
{ loc: { "type": "Point", "coordinates": [75.759973, 26.977889] } },
{ loc: { "type": "Point", "coordinates": [75.771209, 26.97858] } }
]
}
也就是说,每个条目都成为一个对象。我可能想对 odo 和 starttime 做同样的事情。
我正在尝试聚合管道,但除了使用应用程序代码来读取和转换对象之外,我还没有找到其他方法。
我宁愿通过仅 mongo 的方式来做,而不是使用应用程序并逐个文档解析文档,因此非常感谢任何有关 mongo-only 方式的帮助。
解决方案
会是这个:
db.collection.aggregate([
{
$set: {
location: "$$REMOVE",
locations: {
$map: {
input: "$location",
in: { loc: "$$this" }
}
}
}
}
])
推荐阅读
- authentication - npm login --always-auth doesn't add the appropriate entry to the config
- python - How do I read a CSV file using Pandas then split the columns into two Numpy arrays using to_numpy for Scikit-Learn?
- azerothcore - 在游戏连接中禁用并允许创建角色 azerothcore
- android - how to delete (photo ,video , file) in android 9
- acumatica - 如何在网格视图委托中覆盖账单和调整屏幕 AddPOReceipt 按钮
- javascript - 为什么在 Dynamics 365 中对 html Web 资源使用 http 请求创建注释后会收到 500 内部服务器错误?
- c++ - 如何在模板参数中允许类类型,只有当它有两个基类时
- python - 在 Pandas 中创建具有多个条件的箱线图
- python - 在 Visual Studio 中使用 Pythonnet (Python for .Net) 的问题
- java - Jenkins artifactory maven 步骤使用 maven 程序集插件(Artifactory plugin 3.5.0)静默失败