1

I upgraded my MongoDB-C# driver, and I couldn't find information about how to create a GeoSpatial index. I have seen many posts using collection.EnsureIndex, but I don't have that available. I used to use the following, where 'collection' is IMongoCollection<>:

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

What is the new way to do this?

shA.t
  • 16,580
  • 5
  • 54
  • 111
user1202839
  • 365
  • 1
  • 5
  • 18

2 Answers2

3

I think this can help you:

var index = Builders<YourCollectionClass>.IndexKeys.Geo2DSphere("Location");
collection.Indexes.CreateOne(index);
// or async version
await collection.Indexes.CreateOneAsync(index);
shA.t
  • 16,580
  • 5
  • 54
  • 111
0
  IndexKeysDefinition<DBGeoObject> keys = "{ location: \"2dsphere\" }";
  var indexModel = new CreateIndexModel<DBGeoObject>(keys);

  _geoCollection.Indexes.CreateOneAsync(indexModel);

You'll have as a result:

enter image description here

Danil
  • 701
  • 8
  • 7