Search code examples
c#.netmongodb-.net-drivermongodb-csharp-2.0

MongoDB C# Driver -- Create Index


I am using the MongoDB C# driver to create an index

When my app starts up, it creates the index as below

await collection.Indexes.CreateOneAsync(new BsonDocument("code", 1), new CreateIndexOptions() { Unique = true, Sparse = true });

My question is this: If the index already exists, the index will not be re-created/indexed again, correct?


Solution

  • Yes.

    As long as the parameters don't change (e.g "code" becomes "Code" or Sparse becomes false) the index will not be recreated and the operation will be a no-op.