lib/operations/db_ops.js
function createIndex(db, name, fieldOrSpec, options, callback) {
  // Get the write concern options
  let finalOptions = Object.assign({}, { readPreference: ReadPreference.PRIMARY }, options);
  finalOptions = applyWriteConcern(finalOptions, { db }, options);

  // Ensure we have a callback
  if (finalOptions.writeConcern && typeof callback !== 'function') {
    throw MongoError.create({
      message: 'Cannot use a writeConcern without a provided callback',
      driver: true
    });
  }

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

  // Attempt to run using createIndexes command
  createIndexUsingCreateIndexes(db, name, fieldOrSpec, finalOptions, (err, result) => {
    if (err == null) return handleCallback(callback, err, result);

    /**
     * The following errors mean that the server recognized `createIndex` as a command so we don't need to fallback to an insert:
     * 67 = 'CannotCreateIndex' (malformed index options)
     * 85 = 'IndexOptionsConflict' (index already exists with different options)
     * 86 = 'IndexKeySpecsConflict' (index already exists with the same name)
     * 11000 = 'DuplicateKey' (couldn't build unique index because of dupes)
     * 11600 = 'InterruptedAtShutdown' (interrupted at shutdown)
     * 197 = 'InvalidIndexSpecificationOption' (`_id` with `background: true`)
     */
    if (
      err.code === 67 ||
      err.code === 11000 ||
      err.code === 85 ||
      err.code === 86 ||
      err.code === 11600 ||
      err.code === 197
    ) {
      return handleCallback(callback, err, result);
    }

    // Create command
    const doc = createCreateIndexCommand(db, name, fieldOrSpec, options);
    // Set no key checking
    finalOptions.checkKeys = false;
    // Insert document
    db.s.topology.insert(
      `${db.s.databaseName}.${CONSTANTS.SYSTEM_INDEX_COLLECTION}`,
      doc,
      finalOptions,
      (err, result) => {
        if (callback == null) return;
        if (err) return handleCallback(callback, err);
        if (result == null) return handleCallback(callback, null, null);
        if (result.result.writeErrors)
          return handleCallback(callback, MongoError.create(result.result.writeErrors[0]), null);
        handleCallback(callback, null, doc.name);
      }
    );
  });
}
function createIndex(db, name, fieldOrSpec, options, callback) {
  // Get the write concern options
  let finalOptions = Object.assign({}, { readPreference: ReadPreference.PRIMARY }, options);
  finalOptions = applyWriteConcern(finalOptions, { db }, options);

  // Ensure we have a callback
  if (finalOptions.writeConcern && typeof callback !== 'function') {
    throw MongoError.create({
      message: 'Cannot use a writeConcern without a provided callback',
      driver: true
    });
  }

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

  // Attempt to run using createIndexes command
  createIndexUsingCreateIndexes(db, name, fieldOrSpec, finalOptions, (err, result) => {
    if (err == null) return handleCallback(callback, err, result);

    /**
     * The following errors mean that the server recognized `createIndex` as a command so we don't need to fallback to an insert:
     * 67 = 'CannotCreateIndex' (malformed index options)
     * 85 = 'IndexOptionsConflict' (index already exists with different options)
     * 86 = 'IndexKeySpecsConflict' (index already exists with the same name)
     * 11000 = 'DuplicateKey' (couldn't build unique index because of dupes)
     * 11600 = 'InterruptedAtShutdown' (interrupted at shutdown)
     * 197 = 'InvalidIndexSpecificationOption' (`_id` with `background: true`)
     */
    if (
      err.code === 67 ||
      err.code === 11000 ||
      err.code === 85 ||
      err.code === 86 ||
      err.code === 11600 ||
      err.code === 197
    ) {
      return handleCallback(callback, err, result);
    }

    // Create command
    const doc = createCreateIndexCommand(db, name, fieldOrSpec, options);
    // Set no key checking
    finalOptions.checkKeys = false;
    // Insert document
    db.s.topology.insert(
      `${db.s.databaseName}.${CONSTANTS.SYSTEM_INDEX_COLLECTION}`,
      doc,
      finalOptions,
      (err, result) => {
        if (callback == null) return;
        if (err) return handleCallback(callback, err);
        if (result == null) return handleCallback(callback, null, null);
        if (result.result.writeErrors)
          return handleCallback(callback, MongoError.create(result.result.writeErrors[0]), null);
        handleCallback(callback, null, doc.name);
      }
    );
  });
}
function createIndex ( db , name , fieldOrSpec , options , callback ) { // Get the write concern options let finalOptions = Object . assign ( { } , { readPreference : ReadPreference . PRIMARY } , options ) ; finalOptions = applyWriteConcern ( finalOptions , { db } , options ) ; // Ensure we have a callback if ( finalOptions . writeConcern && typeof callback !== 'function' ) { throw MongoError . create ( { message : 'Cannot use a writeConcern without a provided callback' , driver : true } ) ; } // Did the user destroy the topology if ( db . serverConfig && db . serverConfig . isDestroyed ( ) ) return callback ( new MongoError ( 'topology was destroyed' ) ) ; // Attempt to run using createIndexes command createIndexUsingCreateIndexes ( db , name , fieldOrSpec , finalOptions , ( err , result ) => { if ( err == null ) return handleCallback ( callback , err , result ) ; /**
     * The following errors mean that the server recognized `createIndex` as a command so we don't need to fallback to an insert:
     * 67 = 'CannotCreateIndex' (malformed index options)
     * 85 = 'IndexOptionsConflict' (index already exists with different options)
     * 86 = 'IndexKeySpecsConflict' (index already exists with the same name)
     * 11000 = 'DuplicateKey' (couldn't build unique index because of dupes)
     * 11600 = 'InterruptedAtShutdown' (interrupted at shutdown)
     * 197 = 'InvalidIndexSpecificationOption' (`_id` with `background: true`)
     */ if ( err . code === 67 || err . code === 11000 || err . code === 85 || err . code === 86 || err . code === 11600 || err . code === 197 ) { return handleCallback ( callback , err , result ) ; } // Create command const doc = createCreateIndexCommand ( db , name , fieldOrSpec , options ) ; // Set no key checking finalOptions . checkKeys = false ; // Insert document db . s . topology . insert ( ` ${ db . s . databaseName } ${ CONSTANTS . SYSTEM_INDEX_COLLECTION } ` , doc , finalOptions , ( err , result ) => { if ( callback == null ) return ; if ( err ) return handleCallback ( callback , err ) ; if ( result == null ) return handleCallback ( callback , null , null ) ; if ( result . result . writeErrors ) return handleCallback ( callback , MongoError . create ( result . result . writeErrors [ 0 ] ) , null ) ; handleCallback ( callback , null , doc . name ) ; } ) ; } ) ; }
Creates an index on the db and collection.
@method
@param {Db} db The Db instance on which to create an index.
@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
Creates an index on the db and collection .