lib/operations/db_ops.js
function createIndexUsingCreateIndexes(db, name, fieldOrSpec, options, callback) {
  // Build the index
  const indexParameters = parseIndexOptions(fieldOrSpec);
  // Generate the index name
  const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
  // Set up the index
  const indexes = [{ name: indexName, key: indexParameters.fieldHash }];
  // merge all the options
  const keysToOmit = Object.keys(indexes[0]).concat([
    'writeConcern',
    'w',
    'wtimeout',
    'j',
    'fsync',
    'readPreference',
    'session'
  ]);

  for (let optionName in options) {
    if (keysToOmit.indexOf(optionName) === -1) {
      indexes[0][optionName] = options[optionName];
    }
  }

  // Get capabilities
  const capabilities = db.s.topology.capabilities();

  // Did the user pass in a collation, check if our write server supports it
  if (indexes[0].collation && capabilities && !capabilities.commandsTakeCollation) {
    // Create a new error
    const error = new MongoError('server/primary/mongos does not support collation');
    error.code = 67;
    // Return the error
    return callback(error);
  }

  // Create command, apply write concern to command
  const cmd = applyWriteConcern({ createIndexes: name, indexes }, { db }, options);

  // ReadPreference primary
  options.readPreference = ReadPreference.PRIMARY;

  // Build the command
  executeCommand(db, cmd, options, (err, result) => {
    if (err) return handleCallback(callback, err, null);
    if (result.ok === 0) return handleCallback(callback, toError(result), null);
    // Return the indexName for backward compatibility
    handleCallback(callback, null, indexName);
  });
}
function createIndexUsingCreateIndexes(db, name, fieldOrSpec, options, callback) {
  // Build the index
  const indexParameters = parseIndexOptions(fieldOrSpec);
  // Generate the index name
  const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
  // Set up the index
  const indexes = [{ name: indexName, key: indexParameters.fieldHash }];
  // merge all the options
  const keysToOmit = Object.keys(indexes[0]).concat([
    'writeConcern',
    'w',
    'wtimeout',
    'j',
    'fsync',
    'readPreference',
    'session'
  ]);

  for (let optionName in options) {
    if (keysToOmit.indexOf(optionName) === -1) {
      indexes[0][optionName] = options[optionName];
    }
  }

  // Get capabilities
  const capabilities = db.s.topology.capabilities();

  // Did the user pass in a collation, check if our write server supports it
  if (indexes[0].collation && capabilities && !capabilities.commandsTakeCollation) {
    // Create a new error
    const error = new MongoError('server/primary/mongos does not support collation');
    error.code = 67;
    // Return the error
    return callback(error);
  }

  // Create command, apply write concern to command
  const cmd = applyWriteConcern({ createIndexes: name, indexes }, { db }, options);

  // ReadPreference primary
  options.readPreference = ReadPreference.PRIMARY;

  // Build the command
  executeCommand(db, cmd, options, (err, result) => {
    if (err) return handleCallback(callback, err, null);
    if (result.ok === 0) return handleCallback(callback, toError(result), null);
    // Return the indexName for backward compatibility
    handleCallback(callback, null, indexName);
  });
}
function createIndexUsingCreateIndexes ( db , name , fieldOrSpec , options , callback ) { // Build the index const indexParameters = parseIndexOptions ( fieldOrSpec ) ; // Generate the index name const indexName = typeof options . name === 'string' ? options . name : indexParameters . name ; // Set up the index const indexes = [ { name : indexName , key : indexParameters . fieldHash } ] ; // merge all the options const keysToOmit = Object . keys ( indexes [ 0 ] ) . concat ( [ 'writeConcern' , 'w' , 'wtimeout' , 'j' , 'fsync' , 'readPreference' , 'session' ] ) ; for ( let optionName in options ) { if ( keysToOmit . indexOf ( optionName ) === - 1 ) { indexes [ 0 ] [ optionName ] = options [ optionName ] ; } } // Get capabilities const capabilities = db . s . topology . capabilities ( ) ; // Did the user pass in a collation, check if our write server supports it if ( indexes [ 0 ] . collation && capabilities && ! capabilities . commandsTakeCollation ) { // Create a new error const error = new MongoError ( 'server/primary/mongos does not support collation' ) ; error . code = 67 ; // Return the error return callback ( error ) ; } // Create command, apply write concern to command const cmd = applyWriteConcern ( { createIndexes : name , indexes } , { db } , options ) ; // ReadPreference primary options . readPreference = ReadPreference . PRIMARY ; // Build the command executeCommand ( db , cmd , options , ( err , result ) => { if ( err ) return handleCallback ( callback , err , null ) ; if ( result . ok === 0 ) return handleCallback ( callback , toError ( result ) , null ) ; // Return the indexName for backward compatibility handleCallback ( callback , null , indexName ) ; } ) ; }
Create index using the createIndexes command.

@param {Db} db The Db instance on which to execute the command.
@param {string} name Name of the collection to create the index on.
@param {(string|object)} fieldOrSpec Defines the index.
@param {Object} [options] Optional settings. See Db.prototype.createIndex for a list of options.
@param {Db~resultCallback} [callback] The command result callback.
Create index using the createIndexes command .