public function testUnacknowledgedWriteConcern()
 {
     $document = ['x' => 11];
     $options = ['writeConcern' => new WriteConcern(0)];
     $operation = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document, $options);
     $result = $operation->execute($this->getPrimaryServer());
     $this->assertFalse($result->isAcknowledged());
     return $result;
 }
 public function testDropExistingCollection()
 {
     $server = $this->getPrimaryServer();
     $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
     $writeResult = $insertOne->execute($server);
     $this->assertEquals(1, $writeResult->getInsertedCount());
     $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
     $operation->execute($server);
     $this->assertCollectionDoesNotExist($server, $this->getDatabaseName(), $this->getCollectionName());
 }
 public function testListCollectionsForNewlyCreatedDatabase()
 {
     $server = $this->getPrimaryServer();
     $operation = new DropDatabase($this->getDatabaseName());
     $operation->execute($server);
     $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
     $writeResult = $insertOne->execute($server);
     $this->assertEquals(1, $writeResult->getInsertedCount());
     $operation = new ListCollections($this->getDatabaseName(), ['filter' => ['name' => $this->getCollectionName()]]);
     // Convert the CollectionInfoIterator to an array since we cannot rewind its cursor
     $collections = iterator_to_array($operation->execute($server));
     $this->assertCount(1, $collections);
     foreach ($collections as $collection) {
         $this->assertInstanceOf('MongoDB\\Model\\CollectionInfo', $collection);
         $this->assertEquals($this->getCollectionName(), $collection->getName());
     }
 }
 public function testListIndexesForNewlyCreatedCollection()
 {
     $server = $this->getPrimaryServer();
     $operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName());
     $operation->execute($server);
     $insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
     $writeResult = $insertOne->execute($server);
     $this->assertEquals(1, $writeResult->getInsertedCount());
     $operation = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
     // Convert the CursorInfoIterator to an array since we cannot rewind its cursor
     $indexes = iterator_to_array($operation->execute($server));
     $this->assertCount(1, $indexes);
     foreach ($indexes as $index) {
         $this->assertInstanceOf('MongoDB\\Model\\IndexInfo', $index);
         $this->assertEquals(['_id' => 1], $index->getKey());
     }
 }
Exemplo n.º 5
0
 /**
  * Inserts one document.
  *
  * @see InsertOne::__construct() for supported options
  * @see http://docs.mongodb.org/manual/reference/command/insert/
  * @param array|object $document The document to insert
  * @param array        $options  Command options
  * @return InsertOneResult
  */
 public function insertOne($document, array $options = [])
 {
     if (!isset($options['writeConcern'])) {
         $options['writeConcern'] = $this->writeConcern;
     }
     $operation = new InsertOne($this->databaseName, $this->collectionName, $document, $options);
     $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
     return $operation->execute($server);
 }