function extractFields(
  document,
  { fieldsToIgnore = [], alsoExtractValues = false } = {},
  { path = null, extractedFields = [] } = {},
) {
  for (const [key, value] of Object.entries(document)) {
    if (
      fieldsToIgnore.length &&
      fieldsToIgnore.find((keyToIgnore) => keyToIgnore === key)
    ) {
      continue;
    }

    const currentPath = path ? `${path}.${key}` : key;

    if (isPlainObject(value)) {
      extractFields(
        value,
        { alsoExtractValues, fieldsToIgnore },
        { extractedFields, path: currentPath },
      );
    } else if (alsoExtractValues) {
      extractedFields.push({ key: currentPath, value });
    } else {
      extractedFields.push(currentPath);
    }
  }

  return extractedFields;
}
Extract nested fields of an object in a flat array.
Example: { field : { 1: "nested", 2: "nested" } } => [ 'field.1', 'field.2' ]

@param {Object} document - Document to extract fields from
@param {Object} options - alsoExtractValues (false), fieldsToIgnore ( [ ] )

@returns { Array<String> | Array<{ key: String, value: any }> }