C#MongoDB驱动程序2.4 - 如何添加GeoSpatial索引
问题描述:
我升级了我的MongoDB-C#驱动程序,但找不到有关如何创建GeoSpatial索引的信息。我看到很多帖子使用collection.EnsureIndex
,但我没有这个可用。 我用以下,其中“collection
”是IMongoCollection<>
:C#MongoDB驱动程序2.4 - 如何添加GeoSpatial索引
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);