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

Create index returns wrong result

      When creating an index that already exists, the IndexManager CreateOne method returns wrong result. Example:

      var client = new MongoClient("mongodb://localhost");
      var db = client.GetDatabase("test");
      
      db.DropCollection("test");
      var collection = db.GetCollection<BsonDocument>("test");
      
      var result1 = collection.Indexes.CreateOne(Builders<BsonDocument>.IndexKeys.Ascending("field1"), new CreateIndexOptions { Name = "index1" });
      var result2 = collection.Indexes.CreateOne(Builders<BsonDocument>.IndexKeys.Ascending("field1"), new CreateIndexOptions { Name = "index2" });
      
      Console.WriteLine($"result1: {result1}");
      Console.WriteLine($"result2: {result2}");
      
      Console.WriteLine("Indexes:");
      foreach (var item in collection.Indexes.List().ToList())
      {
          Console.WriteLine($"\t{item.GetElement("name").Value.AsString}");
      }
      
      //Output:
      //result1: index1
      //result2: index2
      //Indexes:
      //  _id_
      //  index1
      

      The second row of index creation should return Index1 or at least notify me that index was not created since it already exists.

      When doing the same direct in mongo shell I get notified that the index was not created.
      Example:

      print("drop collection")
      db.test.drop()
      print("create collection")
      db.createCollection("test")
      print("create index with name index1")
      db.test.createIndex({"field1":1}, { name : "index1" })
      print("create index with name index2")
      db.test.createIndex({"field1":1}, { name : "index2" })
      print("get indexes")
      db.test.getIndexes()
      
      //Output:
      
      drop collection
      true
      create collection
      { "ok" : 1, "operationTime" : Timestamp(1516718304, 2) }
      create index with name index1
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 1,
      	"numIndexesAfter" : 2,
      	"ok" : 1,
      	"operationTime" : Timestamp(1516718304, 3)
      }
      create index with name index2
      {
      	"createdCollectionAutomatically" : false,
      	"numIndexesBefore" : 2,
      	"numIndexesAfter" : 2,
      	*"note" : "all indexes already exist",*
      	"ok" : 1,
      	"operationTime" : Timestamp(1516718304, 3)
      }
      get indexes
      [
      	{
      		"v" : 2,
      		"key" : {
      			"_id" : 1
      		},
      		"name" : "_id_",
      		"ns" : "test.test"
      	},
      	{
      		"v" : 2,
      		"key" : {
      			"field1" : 1
      		},
      		"name" : "index1",
      		"ns" : "test.test"
      	}
      ]
      
      

            Assignee:
            vincent.kam@mongodb.com Vincent Kam (Inactive)
            Reporter:
            daniel.moqvist@knowit.se daniel moqvist
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: