我的 MongoDB 集合包含從 .csv 檔案匯入的 83,000 多個檔案。該模式是一個簡單的、非嵌套的模式。每個檔案都有一個字串型別的“坐標”欄位,就像
coordinates: "61.11736,-149.86379"
我想更新/替換它們的所有值,如下所示:
coordinates: [61.11736, -149.86379]因為我想使用 MongoDB $near 查詢選擇器,因為它只適用于 with 的type: 'Point'欄位<field_name>: [<long>, <lat>]。
我應該運行哪種查詢來執行此更新?(我正在使用 Node.js/Express)。提前致謝。
我基本上想進行一個查詢,該查詢將獲取靠近指定坐標集的位置(即 MonogoDB 檔案): [long, lat] 但我無法這樣做。
uj5u.com熱心網友回復:
您可以使用聚合管道執行更新,以將欄位轉換為 GeoJson 點物件。
db.geoJson.update({},
[
{
"$addFields": {
"coordinates": {
type: "Point",
coordinates: {
"$map": {
"input": {
"$reverseArray": {
"$split": [
"$coordinates",
","
]
}
},
"as": "c",
"in": {
"$convert": {
"input": "$$c",
"to": "double",
"onError": "",
"onNull": ""
}
}
}
}
}
}
}
],
{
multi: true
})
蒙戈游樂場
之后,您可以在欄位坐標上創建二維球體索引來執行$near您想要的查詢。
請注意,在創建 2dsphere 索引時,坐標被交換以迎合修復潛在的“經度/緯度超出范圍”錯誤。
參考:https ://database.guide/fix-longitude-latitude-is-out-of-bounds-in-mongodb-when-creating-a-2dsphere-index/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/522419.html
上一篇:生成字串的多個可能字謎之一
