PHP code example of mongodb / symfony-bundle

1. Go to this page and download the library: Download mongodb/symfony-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mongodb / symfony-bundle example snippets


// config/bundles.php



return [
    // ...
    MongoDB\Bundle\MongoDBBundle::class => ['all' => true],
];

use MongoDB\Client;

class MyService
{
    public function __construct(
        private Client $client,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireClient;
use MongoDB\Client;

class MyService
{
    public function __construct(
        // Will autowire the client with the id "second"
        private Client $secondClient,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireClient;
use MongoDB\Client;

class MyService
{
    public function __construct(
        #[AutowireClient('second')]
        private Client $client,
    ) {}
}

use MongoDB\Client;
use MongoDB\Database;

class MyService
{
    private Database $database;

    public function __construct(
        Client $client,
    ) {
        $this->database = $client->selectDatabase('myDatabase');
    }
}

use MongoDB\Bundle\Attribute\AutowireDatabase;
use MongoDB\Database;

class MyService
{
    public function __construct(
        #[AutowireDatabase('myDatabase')]
        private Database $database,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireDatabase;
use MongoDB\Database;

class MyService
{
    public function __construct(
        #[AutowireDatabase(database: 'myDatabase', client: 'second')]
        private Database $database,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireCollection;
use MongoDB\Collection;

class MyService
{
    public function __construct(
        #[AutowireCollection(
            database: 'myDatabase',
            collection: 'myCollection'
        )]
        private Collection $collection,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireCollection;
use MongoDB\Collection;

class MyService
{
    public function __construct(
        #[AutowireCollection(
            database: 'myDatabase',
        )]
        private Collection $myCollection,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireCollection;
use MongoDB\Collection;

class MyService
{
    public function __construct(
        #[AutowireCollection(
            database: 'myDatabase',
            client: 'second',
        )]
        private Collection $myCollection,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireCollection;
use MongoDB\Collection;

class MyService
{
    public function __construct(
        #[AutowireCollection]
        private Collection $myCollection,
    ) {}
}

use MongoDB\Bundle\Attribute\AutowireCollection;
use MongoDB\Collection;
use MongoDB\Driver\ReadPreference;

class MyService
{
    public function __construct(
        #[AutowireCollection(codec: Codec::class, readPreference: new ReadPreference('secondary'))]
        private Collection $myCollection,
    ) {}
}