function suggestIndex({
  filter,
  sort,
  maxFields = 6
}) {
  const filterQueries = (0, _query.createDbQueriesFromObject)((0, _query.prepareQueryArgs)(filter));
  const filterQueriesThatCanUseIndex = getQueriesThatCanUseIndex(filterQueries);
  const sortFields = getSortFieldsThatCanUseIndex(sort);

  if (!sortFields.length && !filterQueriesThatCanUseIndex.length) {
    return [];
  }

  if (!filterQueriesThatCanUseIndex.length) {
    return dedupeAndTrim(sortFields, maxFields);
  }

  if (!sortFields.length) {
    return dedupeAndTrim(toIndexFields(filterQueriesThatCanUseIndex), maxFields);
  } // Combined index for filter+sort only makes sense when all prefix fields have `eq` predicate
  // Same as https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/#sort-and-non-prefix-subset-of-an-index


  const sortDirection = sortFields[0][1];
  const eqFilterQueries = getEqQueries(filterQueriesThatCanUseIndex);
  const eqFilterFields = toIndexFields(eqFilterQueries, sortDirection); // Index prefix should not contain eq filters overlapping with sort fields

  const overlap = findOverlappingFields(eqFilterQueries, sortFields);
  return dedupeAndTrim([...eqFilterFields.filter(([name]) => !overlap.has(name)), ...sortFields, // Still append other filter fields to the tail of the index to leverage additional filtering
  //  of results using data stored in the index (without loading full node object)
  //  Note: fields previously listed in eqFilterFields and sortFields will be removed in dedupeAndTrim
  ...toIndexFields(filterQueriesThatCanUseIndex, sortDirection)], maxFields);
}
/**
 * Suggest index fields for this combination of filter and sort.
 *
 * Prioritizes sort fields over filter fields when can't use index
 * for both because sorting is expensive both CPU and memory-wise.
 */
Suggest index fields for this combination of filter and sort.
Prioritizes sort fields over filter fields when can't use index
for both because sorting is expensive both CPU and memory-wise.
Suggest index fields for this combination of filter and sort . Prioritizes sort fields over filter fields when can ' t use index for both because sorting is expensive both CPU and memory - wise .
function suggestIndex({
  filter,
  sort,
  maxFields = 6
}) {
  const filterQueries = (0, _query.createDbQueriesFromObject)((0, _query.prepareQueryArgs)(filter));
  const filterQueriesThatCanUseIndex = getQueriesThatCanUseIndex(filterQueries);
  const sortFields = getSortFieldsThatCanUseIndex(sort);
  if (!sortFields.length && !filterQueriesThatCanUseIndex.length) {
    return [];
  }
  if (!filterQueriesThatCanUseIndex.length) {
    return dedupeAndTrim(sortFields, maxFields);
  }
  if (!sortFields.length) {
    return dedupeAndTrim(toIndexFields(filterQueriesThatCanUseIndex), maxFields);
  } 
  const sortDirection = sortFields[0][1];
  const eqFilterQueries = getEqQueries(filterQueriesThatCanUseIndex);
  const eqFilterFields = toIndexFields(eqFilterQueries, sortDirection); 
  const overlap = findOverlappingFields(eqFilterQueries, sortFields);
  return dedupeAndTrim([...eqFilterFields.filter(([name]) => !overlap.has(name)), ...sortFields, 
  ...toIndexFields(filterQueriesThatCanUseIndex, sortDirection)], maxFields);
}
function suggestIndex ( { filter , sort , maxFields = 6 } ) { const filterQueries = ( 0 , _query . createDbQueriesFromObject ) ( ( 0 , _query . prepareQueryArgs ) ( filter ) ) ; const filterQueriesThatCanUseIndex = getQueriesThatCanUseIndex ( filterQueries ) ; const sortFields = getSortFieldsThatCanUseIndex ( sort ) ; if ( ! sortFields . length && ! filterQueriesThatCanUseIndex . length ) { return [ ] ; } if ( ! filterQueriesThatCanUseIndex . length ) { return dedupeAndTrim ( sortFields , maxFields ) ; } if ( ! sortFields . length ) { return dedupeAndTrim ( toIndexFields ( filterQueriesThatCanUseIndex ) , maxFields ) ; } const sortDirection = sortFields [ 0 ] [ 1 ] ; const eqFilterQueries = getEqQueries ( filterQueriesThatCanUseIndex ) ; const eqFilterFields = toIndexFields ( eqFilterQueries , sortDirection ) ; const overlap = findOverlappingFields ( eqFilterQueries , sortFields ) ; return dedupeAndTrim ( [ ... eqFilterFields . filter ( ( [ name ] ) => ! overlap . has ( name ) ) , ... sortFields , ... toIndexFields ( filterQueriesThatCanUseIndex , sortDirection ) ] , maxFields ) ; }
Suggest index fields for this combination of filter and sort.
Suggest index fields for this combination of filter and sort .
// Combined index for filter+sort only makes sense when all prefix fields have `eq` predicate // Same as https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/#sort-and-non-prefix-subset-of-an-index // Index prefix should not contain eq filters overlapping with sort fields // Still append other filter fields to the tail of the index to leverage additional filtering //  of results using data stored in the index (without loading full node object) //  Note: fields previously listed in eqFilterFields and sortFields will be removed in dedupeAndTrim