MongoDb c# -如何在文档中存储GeoJSON

本文关键字:文档 存储 GeoJSON MongoDb | 更新日期: 2023-09-27 18:10:20

在MongoDb c# Driver中很难找到GeoJSON

我试着创建一个这样的文档:

new BsonDocument()
{
    { "PointType", "Building"},
    { "Name", "My Place"},
    { "Location", ??? }
}

并尝试在上面的???中填充类似GeoJson.Point(new GeoJson2DCoordinates(0.0000, 0.0000)).ToBsonDocument()的内容。

但是一旦我为Location创建索引并尝试插入此文档,MongoDb就不高兴了。它抛出location object expected, location array not in correct format

是否有BsonDocument和GeoJSON的c#样本或文档?

还有其他插入GeoJSON值的建议吗?

MongoDb c# -如何在文档中存储GeoJSON

我应该使用IndexKeys.Geo2DSphere而不是IndexKeys.Geo2D来存储GeoJSON点。

var keys = Builders<BsonDocument>.IndexKeys.Geo2DSphere("Location");
await collection.Indexes.CreateOneAsync(keys);

然后我可以做:

new BsonDocument()
{
    { "PointType", "Building"},
    { "Name", "My Place"},
    { "Location", GeoJson.Point(new GeoJson2DCoordinates(lng, lat)).ToBsonDocument() }
}