lib/operations/db_ops.js
function createCreateIndexCommand(db, name, fieldOrSpec, options) {
  const indexParameters = parseIndexOptions(fieldOrSpec);
  const fieldHash = indexParameters.fieldHash;

  // Generate the index name
  const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
  const selector = {
    ns: db.databaseName + '.' + name,
    key: fieldHash,
    name: indexName
  };

  // Ensure we have a correct finalUnique
  const finalUnique = options == null || 'object' === typeof options ? false : options;
  // Set up options
  options = options == null || typeof options === 'boolean' ? {} : options;

  // Add all the options
  const keysToOmit = Object.keys(selector);
  for (let optionName in options) {
    if (keysToOmit.indexOf(optionName) === -1) {
      selector[optionName] = options[optionName];
    }
  }

  if (selector['unique'] == null) selector['unique'] = finalUnique;

  // Remove any write concern operations
  const removeKeys = ['w', 'wtimeout', 'j', 'fsync', 'readPreference', 'session'];
  for (let i = 0; i < removeKeys.length; i++) {
    delete selector[removeKeys[i]];
  }

  // Return the command creation selector
  return selector;
}
function createCreateIndexCommand(db, name, fieldOrSpec, options) {
  const indexParameters = parseIndexOptions(fieldOrSpec);
  const fieldHash = indexParameters.fieldHash;

  // Generate the index name
  const indexName = typeof options.name === 'string' ? options.name : indexParameters.name;
  const selector = {
    ns: db.databaseName + '.' + name,
    key: fieldHash,
    name: indexName
  };

  // Ensure we have a correct finalUnique
  const finalUnique = options == null || 'object' === typeof options ? false : options;
  // Set up options
  options = options == null || typeof options === 'boolean' ? {} : options;

  // Add all the options
  const keysToOmit = Object.keys(selector);
  for (let optionName in options) {
    if (keysToOmit.indexOf(optionName) === -1) {
      selector[optionName] = options[optionName];
    }
  }

  if (selector['unique'] == null) selector['unique'] = finalUnique;

  // Remove any write concern operations
  const removeKeys = ['w', 'wtimeout', 'j', 'fsync', 'readPreference', 'session'];
  for (let i = 0; i < removeKeys.length; i++) {
    delete selector[removeKeys[i]];
  }

  // Return the command creation selector
  return selector;
}
function createCreateIndexCommand ( db , name , fieldOrSpec , options ) { const indexParameters = parseIndexOptions ( fieldOrSpec ) ; const fieldHash = indexParameters . fieldHash ; // Generate the index name const indexName = typeof options . name === 'string' ? options . name : indexParameters . name ; const selector = { ns : db . databaseName + '.' + name , key : fieldHash , name : indexName } ; // Ensure we have a correct finalUnique const finalUnique = options == null || 'object' === typeof options ? false : options ; // Set up options options = options == null || typeof options === 'boolean' ? { } : options ; // Add all the options const keysToOmit = Object . keys ( selector ) ; for ( let optionName in options ) { if ( keysToOmit . indexOf ( optionName ) === - 1 ) { selector [ optionName ] = options [ optionName ] ; } } if ( selector [ 'unique' ] == null ) selector [ 'unique' ] = finalUnique ; // Remove any write concern operations const removeKeys = [ 'w' , 'wtimeout' , 'j' , 'fsync' , 'readPreference' , 'session' ] ; for ( let i = 0 ; i < removeKeys . length ; i ++ ) { delete selector [ removeKeys [ i ] ] ; } // Return the command creation selector return selector ; }
Create the command object for Db.prototype.createIndex.

@param {Db} db The Db instance on which to create 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.
@return {Object} The insert command object.
Create the command object for Db . prototype . createIndex .