public function persist($collection, array $document)
    {
        

        if ($collection instanceof Collection) {
            $collectionName = $collection->getName();
        } else {
            $collectionName = $collection;
            $collection = static::factory($collection);
        }
        
        $data = $this->marshall($document);
        $result = false;

        
        if (!isset($document['$id'])) {
            $id = $this->insert($collectionName, $data);
            if ($id) {
                $data['$id'] = $id;
                $result = $data;
            }
        } else {
            $data['id'] = $document['$id'];
            unset($data['$id']);
            $result = $this->update($collectionName, $data);

            if ($result) {
                $result = $data;
            }
        }

        return $this->unmarshall($result);
    }