'Clean up a PyMongo doc, removing fields that were only fetched
for the sake of MongoEngine\'s implementation, and return it.'

def _get_as_pymongo(self, doc):

    if ('_cls' in doc):
        del doc['_cls']
    fields = self._loaded_fields
    if (fields and ('_id' in doc) and (((fields.value == QueryFieldList.ONLY) and ('_id' not in fields.fields)) or ((fields.value == QueryFieldList.EXCLUDE) and ('_id' in fields.fields)))):
        del doc['_id']
    return doc
