diff --git a/src/Collection.php b/src/Collection.php
index 231a434e62945efa3f94d3a64975af21718b7cfc..4ab04267114490c1e93a941f2ec10dd6565e5aa0 100644
--- a/src/Collection.php
+++ b/src/Collection.php
@@ -285,8 +285,9 @@ class Collection
*/
public function createIndex($key, array $options = [])
{
- $indexOptions = array_diff_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
- $commandOptions = array_intersect_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
+ $commandOptionKeys = ['maxTimeMS' => 1, 'session' => 1, 'writeConcern' => 1];
+ $indexOptions = array_diff_key($options, $commandOptionKeys);
+ $commandOptions = array_intersect_key($options, $commandOptionKeys);
return current($this->createIndexes([['key' => $key] + $indexOptions], $commandOptions));
}
diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php
index 235dba5690ec16743f6759ca26609a4830558228..b5bd3f59f99af693396754646838324ecede2c38 100644
--- a/tests/Collection/CollectionFunctionalTest.php
+++ b/tests/Collection/CollectionFunctionalTest.php
@@ -9,6 +9,8 @@ use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use MongoDB\Operation\MapReduce;
+use MongoDB\Tests\CommandObserver;
+use stdClass;
/**
* Functional tests for the Collection class.
@@ -100,6 +102,35 @@ class CollectionFunctionalTest extends FunctionalTestCase
$this->assertEquals($this->getNamespace(), $this->collection->getNamespace());
}
+ public function testCreateIndexSplitsCommandOptions()
+ {
+ if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
+ $this->markTestSkipped('Sessions are not supported');
+ }
+
+ (new CommandObserver)->observe(
+ function() {
+ $this->collection->createIndex(
+ ['x' => 1],
+ [
+ 'maxTimeMS' => 1000,
+ 'session' => $this->manager->startSession(),
+ 'sparse' => true,
+ 'unique' => true,
+ 'writeConcern' => new WriteConcern(1),
+ ]
+ );
+ },
+ function(stdClass $command) {
+ $this->assertObjectHasAttribute('lsid', $command);
+ $this->assertObjectHasAttribute('maxTimeMS', $command);
+ $this->assertObjectHasAttribute('writeConcern', $command);
+ $this->assertObjectHasAttribute('sparse', $command->indexes[0]);
+ $this->assertObjectHasAttribute('unique', $command->indexes[0]);
+ }
+ );
+ }
+
public function testDrop()
{
$writeResult = $this->collection->insertOne(['x' => 1]);