Creating and deleting

The following command will insert a single $document that contains an array of two key/value pairs, with the key names of isbn and name:

$document = array( "isbn" => "401", "name" => "MongoDB and PHP" );
$result = $collection->insertOne($document);
var_dump($result);

The output from the var_dump($result) command is shown as follows:

MongoDBInsertOneResult Object
(
[writeResult:MongoDBInsertOneResult:private] => MongoDBDriverWriteResult Object
(
[nInserted] => 1
[nMatched] => 0
[nModified] => 0
[nRemoved] => 0
[nUpserted] => 0
[upsertedIds] => Array
(
)

[writeErrors] => Array
(
)

[writeConcernError] =>
[writeConcern] => MongoDBDriverWriteConcern Object
(
)

)

[insertedId:MongoDBInsertOneResult:private] => MongoDBBSONObjectID Object
(
[oid] => 5941ac50aabac9d16f6da142
)

[isAcknowledged:MongoDBInsertOneResult:private] => 1
)

This rather lengthy output contains all the information that we may need. We can get the ObjectId of the document inserted; the number of inserted, matched, modified, removed, and upserted documents by fields prefixed with n; and information about writeError or writeConcernError.

There are also convenience methods in the $result object if we want to get the information:

  • $result->getInsertedCount(): To get the number of inserted objects
  • $result->getInsertedId(): To get the ObjectId of the inserted document

We can also use the ->insertMany() method to insert many documents at once, as follows:

$documentAlpha = array( "isbn" => "402", "name" => "MongoDB and PHP, 2nd Edition" );
$documentBeta = array( "isbn" => "403", "name" => "MongoDB and PHP, revisited" );
$result = $collection->insertMany([$documentAlpha, $documentBeta]);

print_r($result);

The result can be seen as follows:

(
[writeResult:MongoDBInsertManyResult:private] => MongoDBDriverWriteResult Object
(
[nInserted] => 2
[nMatched] => 0
[nModified] => 0
[nRemoved] => 0
[nUpserted] => 0
[upsertedIds] => Array
(
)

[writeErrors] => Array
(
)

[writeConcernError] =>
[writeConcern] => MongoDBDriverWriteConcern Object
(
)

)

[insertedIds:MongoDBInsertManyResult:private] => Array
(
[0] => MongoDBBSONObjectID Object
(
[oid] => 5941ae85aabac9d1d16c63a2
)

[1] => MongoDBBSONObjectID Object
(
[oid] => 5941ae85aabac9d1d16c63a3
)

)

[isAcknowledged:MongoDBInsertManyResult:private] => 1
)

Again, $result->getInsertedCount() will return 2, whereas $result->getInsertedIds() will return an array with the two newly-created ObjectIds:

array(2) {
[0]=>
object(MongoDBBSONObjectID)#13 (1) {
["oid"]=>
string(24) "5941ae85aabac9d1d16c63a2"
}
[1]=>
object(MongoDBBSONObjectID)#14 (1) {
["oid"]=>
string(24) "5941ae85aabac9d1d16c63a3"
}
}

Deleting documents is a similar process to inserting documents, but uses the deleteOne() and deleteMany() methods instead; an example of deleteMany() is shown as follows:

$deleteQuery = array( "isbn" => "401");
$deleteResult = $collection->deleteMany($deleteQuery);
print($deleteResult->getDeletedCount());

The following code block shows the output:

MongoDBDeleteResult Object
(
[writeResult:MongoDBDeleteResult:private] => MongoDBDriverWriteResult Object
(
[nInserted] => 0
[nMatched] => 0
[nModified] => 0
[nRemoved] => 2
[nUpserted] => 0
[upsertedIds] => Array
(
)

[writeErrors] => Array
(
)

[writeConcernError] =>
[writeConcern] => MongoDBDriverWriteConcern Object
(
)

)

[isAcknowledged:MongoDBDeleteResult:private] => 1
)
2

In this example, we used ->getDeletedCount() to get the number of affected documents, which is printed in the last line of the output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset