C# MongoDB 驱动程序 2.4 - 如何添加地理空间索引

C# MongoDB driver 2.4 - How to add a GeoSpatial index

我升级了 MongoDB-C# 驱动程序,但找不到有关如何创建 GeoSpatial 索引的信息。我看过很多使用 collection.EnsureIndex 的帖子,但我没有。 我曾经使用以下内容,其中 'collection' 是 IMongoCollection<>:

collection.CreateIndex((new IndexKeysBuilder().GeoSpatialSpherical("Location")));

执行此操作的新方法是什么?

我认为这可以帮助你:

var index = Builders<YourCollectionClass>.IndexKeys.Geo2DSphere("Location");
collection.Indexes.CreateOne(index);
// or async version
await collection.Indexes.CreateOneAsync(index);