Fusio

Recipes / Mongodb / Find document

Routes
resources/routes.yaml
"/test":
  version: 1
  methods:
    GET:
      public: true
      action: "${dir.src}/mongodb-find.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-find.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');

$entries = $collection->find([], [
    'limit' => 5,
]);

return $response->build(200, [], [
    'entry' => $entries,
]);