MongoDB\Collection::insertOne() (original) (raw)
MongoDB\Collection::insertOne()
Insert one document.
function insertOne(
array|object $document,
array $options = []
): MongoDB\InsertOneResult
$document
: array|object
The document to insert into the collection.
$options
: array
An array specifying the desired options.
Name | Type | Description |
---|---|---|
bypassDocumentValidation | boolean | If true, allows the write operation to circumvent document level validation. Defaults to false. |
codec | MongoDB\Codec\DocumentCodec | The codec to use for encoding or decoding documents. This option is mutually exclusive with the typeMap option.Defaults to the collection's codec. Inheritance for a default codec option takes precedence over that of the typeMap option.New in version 1.17. |
comment | mixed | Enables users to specify an arbitrary comment to help trace the operation through the database profiler,currentOp output, andlogs.This option is available since MongoDB 4.4 and will result in an exception at execution time if specified for an older server version.New in version 1.13. |
session | MongoDB\Driver\Session | Client session to associate with the operation.New in version 1.3. |
writeConcern | MongoDB\Driver\WriteConcern | Write concern to use for the operation. Defaults to the collection's write concern.It is not possible to specify a write concern for individual operations as part of a transaction. Instead, set the writeConcern option whenstarting the transaction. |
A MongoDB\InsertOneResult object, which encapsulates aMongoDB\Driver\WriteResult object.
MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.
MongoDB\Driver\Exception\BulkWriteException for errors related to the write operation. Users should inspect the value returned by getWriteResult() to determine the nature of the error.
MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).
If a MongoDB\Driver\Exception\BulkWriteException is thrown, users should callgetWriteResult() and inspect the returned MongoDB\Driver\WriteResult object to determine the nature of the error.
For example, a write operation may have been successfully applied to the primary server but failed to satisfy the write concern (e.g. replication took too long). Alternatively, a write operation may have failed outright (e.g. unique key violation).
The following operation inserts a document into the users
collection in thetest
database:
<?php
$collection = (new MongoDB\Client)->test->users;
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>s</mi><mi>e</mi><mi>r</mi><mi>t</mi><mi>O</mi><mi>n</mi><mi>e</mi><mi>R</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">insertOneResult = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mord mathnormal" style="margin-right:0.02778em;">tO</span><span class="mord mathnormal">n</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="mord mathnormal">es</span><span class="mord mathnormal">u</span><span class="mord mathnormal">lt</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>collection->insertOne([
'username' => 'admin',
'email' => 'admin@example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
The output would then resemble:
Inserted 1 document(s)
object(MongoDB\BSON\ObjectId)#11 (1) {
["oid"]=>
string(24) "579a25921f417dd1e5518141"
}
- MongoDB\Collection::insertMany()
- MongoDB\Collection::bulkWrite()
- Insert Documents
- insert command reference in the MongoDB manual