Mongodb master slave connection

Hi YiiTeam,

How to connect mongodb with master slave config ?

My db will be on different servers.

If master is down then it will be switch between server and i have to select my master db using PHP. Please guide how to do in yii2-mongodb extension ?


Yatin Mistry

For my solution i have set mongodb replica set. It worked

Below is connection string (i have refered here)

// MongoDB

$config["components"]['mongodb'] = [

    'class' => '\yii\mongodb\Connection',

    'dsn' => 'mongodb://,,',


Hi @yatinmistry5130

I just wanted to check you, if you used read-preferences as well. When I use reapreference as Secondary, I am seeing errors when I read from secondary.