lib/operations/db_ops.js
function ensureIndex(db, name, fieldOrSpec, options, callback) {
  // Get the write concern options
  const finalOptions = applyWriteConcern({}, { db }, options);
  // Create command
  const selector = createCreateIndexCommand(db, name, fieldOrSpec, options);
  const index_name = selector.name;

  // Did the user destroy the topology
  if (db.serverConfig && db.serverConfig.isDestroyed())
    return callback(new MongoError('topology was destroyed'));

  // Merge primary readPreference
  finalOptions.readPreference = ReadPreference.PRIMARY;

  // Check if the index already exists
  indexInformation(db, name, finalOptions, (err, indexInformation) => {
    if (err != null && err.code !== 26) return handleCallback(callback, err, null);
    // If the index does not exist, create it
    if (indexInformation == null || !indexInformation[index_name]) {
      createIndex(db, name, fieldOrSpec, options, callback);
    } else {
      if (typeof callback === 'function') return handleCallback(callback, null, index_name);
    }
  });
}
function ensureIndex(db, name, fieldOrSpec, options, callback) {
  // Get the write concern options
  const finalOptions = applyWriteConcern({}, { db }, options);
  // Create command
  const selector = createCreateIndexCommand(db, name, fieldOrSpec, options);
  const index_name = selector.name;

  // Did the user destroy the topology
  if (db.serverConfig && db.serverConfig.isDestroyed())
    return callback(new MongoError('topology was destroyed'));

  // Merge primary readPreference
  finalOptions.readPreference = ReadPreference.PRIMARY;

  // Check if the index already exists
  indexInformation(db, name, finalOptions, (err, indexInformation) => {
    if (err != null && err.code !== 26) return handleCallback(callback, err, null);
    // If the index does not exist, create it
    if (indexInformation == null || !indexInformation[index_name]) {
      createIndex(db, name, fieldOrSpec, options, callback);
    } else {
      if (typeof callback === 'function') return handleCallback(callback, null, index_name);
    }
  });
}
function ensureIndex ( db , name , fieldOrSpec , options , callback ) { // Get the write concern options const finalOptions = applyWriteConcern ( { } , { db } , options ) ; // Create command const selector = createCreateIndexCommand ( db , name , fieldOrSpec , options ) ; const index_name = selector . name ; // Did the user destroy the topology if ( db . serverConfig && db . serverConfig . isDestroyed ( ) ) return callback ( new MongoError ( 'topology was destroyed' ) ) ; // Merge primary readPreference finalOptions . readPreference = ReadPreference . PRIMARY ; // Check if the index already exists indexInformation ( db , name , finalOptions , ( err , indexInformation ) => { if ( err != null && err . code !== 26 ) return handleCallback ( callback , err , null ) ; // If the index does not exist, create it if ( indexInformation == null || ! indexInformation [ index_name ] ) { createIndex ( db , name , fieldOrSpec , options , callback ) ; } else { if ( typeof callback === 'function' ) return handleCallback ( callback , null , index_name ) ; } } ) ; }
Ensures that an index exists. If it does not, creates it.

@method
@param {Db} db The Db instance on which to ensure the index.
@param {string} name The index name
@param {(string|object)} fieldOrSpec Defines the index.
@param {object} [options] Optional settings. See Db.prototype.ensureIndex for a list of options.
@param {Db~resultCallback} [callback] The command result callback
Ensures that an index exists . If it does not creates it .