Fusio

Recipes / Mongodb / Update document

Routes
resources/routes.yaml
"/test/:id":
  version: 1
  methods:
    PUT:
      public: true
      action: "${dir.src}/mongodb-update.php"
Connection
resources/connections.yaml
Mongodb-Connection:
  class: Fusio\Adapter\Mongodb\Connection\MongoDB
  config:
    url: "mongodb://127.0.0.1"
    database: "app"
Action
resources/mongodb-update.php
<?php
/**
 * @var \Fusio\Engine\ConnectorInterface $connector
 * @var \Fusio\Engine\ContextInterface $context
 * @var \Fusio\Engine\RequestInterface $request
 * @var \Fusio\Engine\Response\FactoryInterface $response
 * @var \Fusio\Engine\ProcessorInterface $processor
 * @var \Psr\Log\LoggerInterface $logger
 * @var \Psr\SimpleCache\CacheInterface $cache
 */

/** @var \MongoDB\Database $connection */
$connection = $connector->getConnection('Mongodb-Connection');
$collection = $connection->selectCollection('app_todo');

$filter = [
    'id' => ['$eq' => $request->getUriFragment('id')]
];

$collection->updateOne($filter, $request->getBody());

return $response->build(200, [], [
    'success' => true,
    'message' => 'Update successful',
]);