code: def _dereference(cls, documents, references):
          
  
          # Dereference each reference
          for path, projection in references.items():
  
              # Check there is a $ref in the projection, else skip it
              if '$ref' not in projection:
                  continue
  
              # Collect Ids of documents to dereference
              ids = set()
              for document in documents:
                  value = cls._path_to_value(path, document)
                  if not value:
                      continue
  
                  if isinstance(value, list):
                      ids.update(value)
  
                  elif isinstance(value, dict):
                      ids.update(value.values())
  
                  else:
                      ids.add(value)
  
              # Find the referenced documents
              ref = projection.pop('$ref')
              frames = ref.many(
                  {'_id': {'$in': list(ids)}},
                  projection=projection
                  )
              frames = {f._id: f for f in frames}
  
              # Add dereferenced frames to the document
              for document in documents:
                  value = cls._path_to_value(path, document)
                  if not value:
                      continue
  
                  if isinstance(value, list):
                      # List of references
                      value = [frames[id] for id in value if id in frames]
  
                  elif isinstance(value, dict):
                      # Dictionary of references
                      value = {key: frames.get(id) for key, id in value.items()}
  
                  else:
                      value = frames.get(value, None)
  
                  child_document = document
                  keys = cls._path_to_keys(path)
                  for key in keys[:-1]:
                      child_document = child_document[key]
                  child_document[keys[-1]] = value
docs: Dereference one or more documents