createIndex() succeeds but index doesn't show with showIndexes()

I’m having a very strange issue, and I suspect that I must be missing something obvious, but I can’t figure out what.

I’m running 5.0.15 in a Docker Desktop k8s cluster, and I can store and retrieve data as expected. However, there are no indexes defined (I’m using Spring Data’s @Indexed annotation). At first I thought that there might be a Spring issue, but I finally tried in mongosh, and I can’t create indexes there, so I think that’s the main problem.

When I run this in mongosh:

obiwan-rest> db.company.getIndexes();
[ { v: 2, key: { _id: 1 }, name: '_id_' } ]
obiwan-rest> db.customer.createIndex({"customerNumber":1},{unique:true});
customerNumber_1
obiwan-rest> db.company.getIndexes();
[ { v: 2, key: { _id: 1 }, name: '_id_' } ]

it seems that createIndex() succeeded, but the created index is not shown. In Compass, I can’t see it either.

When I create the same index in Compass, it does show up. And if I try to create a similar index, for example, by leaving out the unique option, I do get an error message:

obiwan-rest> db.company.getIndexes();
[ { v: 2, key: { _id: 1 }, name: '_id_' } ]
obiwan-rest> db.customer.createIndex({"customerNumber":1});
MongoServerError: An existing index has the same name as the requested index. When index names are not specified, they are auto generated and can cause conflicts. Please refer to our documentation. Requested index: { v: 2, key: { customerNumber: 1 }, name: "customerNumber_1" }, existing index: { v: 2, unique: true, key: { customerNumber: 1 }, name: "customerNumber_1" }

I’m not sure what’s happening here. Is there something missing on the server side? Am I using createIndex() wrong?

The collection has one document in it, so I would expect index creation to be instantaneous. But even waiting 10 minutes does not make the index show up, so I don’t think it’s anything to do with background index creation.

This is what the index looks like after I’ve created it in Compass:

obiwan-rest> db.company.getIndexes();
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { customerNumber: 1 },
    name: 'customerNumber_1',
    unique: true,
    sparse: false
  }
]

This is exactly what I would expect from the shell command, so I’m not sure what Compass does differently.

If I try to drop the index I created in Compass, this happens:

obiwan-rest> db.customer.dropIndex("customerNumber_1");
{ nIndexesWas: 2, ok: 1 }
obiwan-rest> db.company.getIndexes();
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { customerNumber: 1 },
    name: 'customerNumber_1',
    unique: true,
    sparse: false
  }
]

There is something I do not understand in what your are doing. It does not seem a mistake since you do it over and over.

You are wrongly creating and removing the index in one collection and testing the existence of the index in another collection. This seems like a clear misunderstanding of what are indexes.

You create in collection customer

but check in collection company.

And then again check in company

create in customer

You get the error

because you created it in customer before.

However in Compass you seem to created correctly in company as shown by

And then same think again, you are dropping in customer

but checking in company

1 Like

You are absolutely correct, I was copy/pasting commands and obivously had gotten confused about the collection names.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.