Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-215

Java driver's ensureIndex() does not preserve order

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • 2.4
    • Affects Version/s: None
    • Component/s: API
    • None
    • Environment:
      Java
    • Major Change

      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() )

      { if ( name.length() > 0 ) name += "_"; name += s + "_"; Object val = keys.get( s ); if ( val instanceof Number || val instanceof String ) name += val.toString().replace( ' ' , '_' ); }

      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.

            Assignee:
            eliot Eliot Horowitz (Inactive)
            Reporter:
            vijayp Vijay Pandurangan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: