[CSHARP-2529] Get an Index by name Created: 25/Feb/19  Updated: 25/Feb/19  Resolved: 25/Feb/19

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: KevinBrok Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

In our application we are saving messages in a mongodatabase that have been sent out to an external service in case the external server bumps into a problem and the message has to be sent again later. To prevent the collection getting to big we implemented a TTL Index. The timespan given in this Index is configurable via the appsettings.json.

The first time the applications boots the ExpireAfter is set through what is defined in the appsettings.json, e.g. 30 days. If we later on decide that 30 days is to long, we want to be able to adjust it to 15 days. But when we do that now an exception occurs on creating the index with message: "Command createIndexes failed: Index with name: LastUpdatedAt_1 already exists with different options.".

So before creating the index there needs to be checked if the Index already exists with the same options. The only way to do that now is:

var query = await myCollection.Indexes.ListAsync();
var bsonIndexes = await query.ToListAsync();
var indexNames = bsonIndexes .Select(i => i.Values); // or some variation
if(indexNames.Contains("LastUpdatedAt_1"))
{
    // code here
}

As stated in this issue: https://jira.mongodb.org/browse/CSHARP-1455

I would like to be able to check if an index exists in an easier way like:

 

var filter = Builders<MongoIndex>.Filter.Eq(x => x.Name, "LastUpdatedAt_1");
var index = (await Collection.Indexes.FindAsync(filter)).FirstOrDefault();
if (index != null)
{ 
    // code here
}

 



 Comments   
Comment by Ian Whalen (Inactive) [ 25/Feb/19 ]

kevinbrok There's no server support for this and in general we do not add support for a filter client-side. Please open a SERVER ticket if you'd like to see support for this added.

https://docs.mongodb.com/manual/reference/command/listIndexes/

Generated at Wed Feb 07 21:42:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.