[CSHARP-2152] Create index returns wrong result Created: 23/Jan/18  Updated: 27/Oct/23  Resolved: 07/Feb/18

Status: Closed
Project: C# Driver
Component/s: Command Operations, Error Handling, Operations
Affects Version/s: 2.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: daniel moqvist Assignee: Vincent Kam (Inactive)
Resolution: Works as Designed Votes: 0
Labels: Indexing
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows


Issue Links:
Depends
depends on SERVER-33149 createIndexes fails to report an erro... Closed
Server Compat: 4.1

 Description   

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"
	}
]



 Comments   
Comment by daniel moqvist [ 08/Feb/18 ]

Hi Vincent,
We are creating indexes automatically based on the schema of the documents in the collection. This indexing happens every time we start our service to ensure the indexes can reflect any schema update. We don't want to recreate an index if it already exists, so here is a name handy to use. We don't use index hinting at all in our solution. We have to figure out another way to see if the index exists before we try to create it.

Kind regard,
Daniel

Comment by Vincent Kam (Inactive) [ 07/Feb/18 ]

Hello Daniel,

Thank you for your submission! It is somewhat of an interesting problem.

After some investigation, as Robert noted, this unfortunately appears to be a server bug. It is also not feasible to return the existing index name because the server is not returning that information, and it would be undesirable to require an additional round trip to the server to verify whether the existing index has the same name or not.

May I ask what you're using the index name for? Would index hinting via the key pattern work instead of supplying an index name?

For now though, we've notified the server team, but until the bug is fixed, the driver can't do much and is working as designed (since "ok: 1" was returned). I can only offer the "index hinting" workaround as a potential solution, my apologies.

Kind regards,
Vincent

Comment by Robert Stam [ 06/Feb/18 ]

This looks like a server bug.

When creating the second index (with a different name) the server is reporting that the index already exists, even though the existing index has a different name.

See:

https://jira.mongodb.org/browse/SERVER-33149

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