0%

Azure CosmosDB 使用 MongoDB Geolocation

Azure CosmosDB 使用 MongoDB GeoLocation功能

真D好難

首先先到Azure CosmosDb 你的Collection設定內
將includedPaths改成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Range",
"dataType": "String",
"precision": -1
}
]
}

然後再用程式碼加入2dsphere的index

1
2
3
IndexKeysDefinition<BravoPoint> keys = "{ Location: \"2dsphere\" }";
var indexModel = new CreateIndexModel<BravoPoint>(keys);
var s = collection.Indexes.CreateOne(indexModel);

最後可以得出Result

1
2
3
4
var gp = new GeoJsonPoint<GeoJson2DGeographicCoordinates>
(new GeoJson2DGeographicCoordinates(121.575163, 25.078124));
var query = Builders<BravoPoint>.Filter.Near("Location", gp, 10000);
var result = collection.Find(query).ToList();

MongoDb

MongoDB 學習筆記四 C#調用MongoDB