function collectIndexes(schema, prefix, baseSchema) {
    // Ignore infinitely nested schemas, if we've already seen this schema
    // along this path there must be a cycle
    if (schemaStack.has(schema)) {
      return;
    }
    schemaStack.set(schema, true);

    prefix = prefix || '';
    const keys = Object.keys(schema.paths);

    for (const key of keys) {
      const path = schema.paths[key];
      if (baseSchema != null && baseSchema.paths[key]) {
        // If looking at an embedded discriminator schema, don't look at paths
        // that the
        continue;
      }

      if (path._duplicateKeyErrorMessage != null) {
        schema._duplicateKeyErrorMessagesByPath = schema._duplicateKeyErrorMessagesByPath || {};
        schema._duplicateKeyErrorMessagesByPath[key] = path._duplicateKeyErrorMessage;
      }

      if (path.$isMongooseDocumentArray || path.$isSingleNested) {
        if (get(path, 'options.excludeIndexes') !== true &&
            get(path, 'schemaOptions.excludeIndexes') !== true &&
            get(path, 'schema.options.excludeIndexes') !== true) {
          collectIndexes(path.schema, prefix + key + '.');
        }

        if (path.schema.discriminators != null) {
          const discriminators = path.schema.discriminators;
          const discriminatorKeys = Object.keys(discriminators);
          for (const discriminatorKey of discriminatorKeys) {
            collectIndexes(discriminators[discriminatorKey],
              prefix + key + '.', path.schema);
          }
        }

        // Retained to minimize risk of backwards breaking changes due to
        // gh-6113
        if (path.$isMongooseDocumentArray) {
          continue;
        }
      }

      const index = path._index || (path.caster && path.caster._index);

      if (index !== false && index !== null && index !== undefined) {
        const field = {};
        const isObject = helperIsObject(index);
        const options = isObject ? index : {};
        const type = typeof index === 'string' ? index :
          isObject ? index.type :
            false;

        if (type && indexTypes.indexOf(type) !== -1) {
          field[prefix + key] = type;
        } else if (options.text) {
          field[prefix + key] = 'text';
          delete options.text;
        } else {
          let isDescendingIndex = false;
          if (index === 'descending' || index === 'desc') {
            isDescendingIndex = true;
          } else if (index === 'ascending' || index === 'asc') {
            isDescendingIndex = false;
          } else {
            isDescendingIndex = Number(index) === -1;
          }

          field[prefix + key] = isDescendingIndex ? -1 : 1;
        }

        delete options.type;
        if (!('background' in options)) {
          options.background = true;
        }
        if (schema.options.autoIndex != null) {
          options._autoIndex = schema.options.autoIndex;
        }

        const indexName = options && options.name;

        if (typeof indexName === 'string') {
          if (indexByName.has(indexName)) {
            Object.assign(indexByName.get(indexName), field);
          } else {
            indexes.push([field, options]);
            indexByName.set(indexName, field);
          }
        } else {
          indexes.push([field, options]);
          indexByName.set(indexName, field);
        }
      }
    }

    schemaStack.delete(schema);

    if (prefix) {
      fixSubIndexPaths(schema, prefix);
    } else {
      schema._indexes.forEach(function(index) {
        const options = index[1];
        if (!('background' in options)) {
          options.background = true;
        }
        decorateDiscriminatorIndexOptions(schema, options);
      });
      indexes = indexes.concat(schema._indexes);
    }
  }
Gather all indexes defined in the schema, including single nested,
document arrays, and embedded discriminators.
@param {Schema} schema
@api private