Blog, Software Engineering

MongoDB: can only have 1 index plugin / bad index key pattern

While working with MongoDB and the MongoDB Java driver I got the following exception when attempting to iterate over a result set.

com.mongodb.MongoException: can only have 1 index plugin / bad index key pattern
at com.mongodb.MongoException.parse(
at com.mongodb.DBApiLayer$MyCollection.__find(
at com.mongodb.DBCursor._check(
at com.mongodb.DBCursor._hasNext(
at com.mongodb.DBCursor.hasNext(

This exception drove me nuts. I checked the index definition in Java, which looked fine, compared it to the index result in Mongo shell (db.collectionName.getIndexes()), which looked fine too, made several desperate attempts to drop and recreate the index, re-index the collection, etc.
Then I cut down the DB query and recognized that removing my sort() call made a difference. Eventually it turned out that the sort order was specified as a String instead of an Integer, see below:

BasicDBObject query = ...
// BasicDBObject sort = new BasicDBObject("column1", "1").append("column2", "-1");
BasicDBObject sort = new BasicDBObject("column1", 1).append("column2", -1);

1 Comment

Comments are closed.