code: def _flatten_projection(cls, projection):
          
  
          # If `projection` is empty return a full projection based on `_fields`
          if not projection:
              return {f: True for f in cls._fields}, {}, {}
  
          # Flatten the projection
          flat_projection = {}
          references = {}
          subs = {}
          inclusive = True
          for key, value in deepcopy(projection).items():
              if isinstance(value, dict):
  
                  # Build the projection value for the field (allowing for
                  # special mongo directives).
                  project_value = {
                      k: v for k, v in value.items()
                      if k.startswith('$') and k not in ['$ref', '$sub', '$sub.']
                  }
                  if len(project_value) == 0:
                      project_value = True
                  else:
                      inclusive = False
  
                  # Store a reference/sub-frame projection
                  if '$ref' in value:
                      references[key] = value
  
                  elif '$sub' in value or '$sub.' in value:
                      subs[key] = value
  
                      if '$sub' in value:
                          sub_frame = value['$sub']
  
                      if '$sub.' in value:
                          sub_frame = value['$sub.']
  
                      project_value = sub_frame._projection_to_paths(key, value)
  
                  if isinstance(project_value, dict):
                      flat_projection.update(project_value)
  
                  else:
                      flat_projection[key] = project_value
  
              elif key == '$ref':
                  # Strip any $ref key
                  continue
  
              elif key == '$sub' or key == '$sub.':
                  # Strip any $sub key
                  continue
  
              else:
                  # Store the root projection value
                  flat_projection[key] = value
                  inclusive = False
  
          # If only references and sub-frames where specified in the projection
          # then return a full projection based on `_fields`.
          if inclusive:
              flat_projection = {f: True for f in cls._fields}
  
          return flat_projection, references, subs
docs: Flatten a structured projection (structure projections support for
          projections of (to be) dereferenced fields.