private static function convertToMongo(array &$data)
    {
        // Move the ID field where Mongo expects it.
        if (isset($data['id'])) {
            // Create the _id field Mongo expects.
            $data['_id'] = $data['id'];
            // Remove the 'id' field from the data set that will be stored.
            unset($data['id']);
        }

        foreach ($data as $name => &$value) {
            // Ensure that all strings are UTF-8.
            if (is_string($value)) {
                // Convert all strings to UTF-8, otherwise Mongo complains.
                if (mb_detect_encoding($value) != 'UTF-8') {
                    $value = mb_convert_encoding($value, 'UTF-8');
                }
            } elseif ($value instanceof Entity) {
                // Only the presistable fields of Entities are kept.
                $value = $value->getFieldsData();
            } elseif (is_array($value)) {
                // The elements in an array are also checked in case they
                // contain sub-documents.
                self::convertToMongo($value);
            }
        }
    }