Create and delete

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

This is the output:

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
)

The 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, like this:

$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 is:

(
[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 similar to inserting but with the deleteOne() and deleteMany() methods; an example of deleteMany() is shown here:

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

Here is 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 out 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
18.224.43.85