public function getByRef(array $ref, array $parentEntity = [])
    {
        $document = $parentEntity;
        if (strpos($ref['rootDocumentEntityType'], 'parent') === false) {
            if (empty($document['_id']) || $document['_id'] !== $ref['rootDocumentId']) {
                $document = $this->getById($ref['rootDocumentEntityType'], $ref['rootDocumentId']);
            }

            if (count($document) === 0) {
                throw new Exception('Invalid document reference (document cannot be found by Id)');
            }
        }

        $container = $document;
        foreach ($ref['path'] as $pathInDocument) {
            if (!array_key_exists($pathInDocument['field'], $container)) {
                throw new Exception('Could not find the path in document');
            }
            $container = $container[$pathInDocument['field']];
            if (empty($pathInDocument['objectId'])) {
                continue;
            }

            if (!is_array($container)) {
                throw new Exception('Invalid value for path in document');
            }
            $result = array_filter($container, function ($item) use ($pathInDocument) {
                return $item['_id'] === $pathInDocument['objectId'];
            });
            if (count($result) === 0) {
                throw new Exception('Empty result of reference');
            }
            $container = reset($result);
        }
        return $container;
    }