From what I understand, the order of the keys in an index is important, since mongo can use any index that has additional keys after the keys required for a query. Thus index(A,B,C) is different from index(C,B,A)
1. ensureIndex() takes a BasicDBObject as an argument.
2. The code:
public static String genIndexName( DBObject keys ){
String name = "";
for ( String s : keys.keySet() )
return name;
}
3. Generates a name for a DBObject. When there are multiple keys, "key1": 1, "key2":2, keyset() will return the data in arbitrary order. The name will then be the same for each SET of keys.
4. in ensureIndex():
final String name = options.get( "name" ).toString();
if ( _createIndexes.contains( name ) )
return;
Will fail, if any other index with the same keys exist, regardless of order.
5. Even if that does not fail, and a name is manually supplied, createIndex's comment implies that it uses a set of fields, which will not preserve order /** Forces creation of an index on a set of fields, if one does not already exist.
- @param keys an object with a key set of the fields desired for the index
*/
public final void createIndex( final DBObject keys )
throws MongoException { createIndex( keys , defaultOptions( keys ) ); }
6. Mongo behaves correctly when adding indices from the console.