我有一個集合,其中包含諸如
{
"_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-only 的方式來完成它,而不是使用應用程式和逐個檔案決議檔案,所以任何使用 mongo-only 方式的幫助將不勝感激。
uj5u.com熱心網友回復:
會是這個:
db.collection.aggregate([
{
$set: {
location: "$$REMOVE",
locations: {
$map: {
input: "$location",
in: { loc: "$$this" }
}
}
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326761.html
下一篇:無法在mongodb中啟用分片
