diff --git a/mongodb.drush.inc b/mongodb.drush.inc
index 840d36b..e81e909 100644
--- a/mongodb.drush.inc
+++ b/mongodb.drush.inc
@@ -11,12 +11,12 @@
  * Implementation of hook_drush_command().
  */
 function mongodb_drush_command() {
-//   $options['--alias'] = 'The alias defined in the variable mongodb_collections.';
+  $options['--select'] = 'When connecting to replica set select which server to connect to.';

   $items['mongodb-connect'] = array(
     'description' => 'A string for connecting to the mongodb.',
     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
-//     'options' => $options,
+    'options' => $options,
     'arguments' => array(
        'alias' => 'The connection',
     ),
@@ -25,7 +25,7 @@ function mongodb_drush_command() {
   $items['mongodb-cli'] = array(
     'description' => "Open a mongodb command-line interface using Drupal's credentials.",
     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
-//     'options' => $options,
+    'options' => $options,
     'examples' => array(
       '`drush mongodb-connect`' => 'Connect to the mongodb.',
     ),
@@ -44,7 +44,6 @@ function mongodb_drush_command() {
        'alias' => 'The connection',
     ),
     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
-//     'options' => $options,
   );

   $items['mongodb-query'] = array(
@@ -57,9 +56,7 @@ function mongodb_drush_command() {
        'alias' => 'The connection',
        'query' => 'A mongodb query. Mandatory.',
     ),
-//     'options' => array(
-//       '--extra' => 'Add custom options to the mongodb command.',
-//     ) + $options,
+    'options' => $options,
     'aliases' => array('mdbq'),
   );

@@ -122,12 +119,37 @@ function _drush_mongodb_connect($alias) {
     $alias = 'default';
   }
   $connection = $connections[$alias];
-  $host = $connection['host'];
+  // If we are dealing with connection to replica set by default connect
+  // to primary server which might not always be in connections configuration.
+  // With --select option you can specify exactly which server in replica set
+  // to connect to.
+  if (isset($connection['connection_options']['replicaSet'])) {
+    $select = drush_get_option(array('select'), FALSE);
+    $mongo_connection = new Mongo('mongodb://' . $connection['host'], $connection['connection_options']);
+    $members = $mongo_connection->getHosts();
+    $options = array();
+    foreach ($members as $key => $member) {
+      $options[$key] = $key;
+      if ($member['state'] == 1) {
+        $options[$key] .= " PRIMARY";
+        $primary_member = $key;
+      }
+    }
+    if ($select) {
+      $host = drush_choice($options);
+    }
+    else {
+      $host = $primary_member;
+    }
+  }
+  else {
+    $host = $connection['host'];
+  }
   $db = $connection['db'];
-
+
   $query = $host;
   $query .= '/' . $db;
-
+
   $command = 'mongo ' . $query;
   return $command;
 }
