Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2529

Get an Index by name

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Won't Fix
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None

    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
      }

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            kevinbrok KevinBrok
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: