diff --git a/snooty.toml b/snooty.toml index 711874cc..7f35dafe 100644 --- a/snooty.toml +++ b/snooty.toml @@ -18,6 +18,7 @@ toc_landing_pages = [ "/reference/class/MongoDBModelDatabaseInfo", "/reference/class/MongoDBModelIndexInfo", "/get-started", + "/write", ] [substitutions] diff --git a/source/includes/usage-examples/write-code-examples.php b/source/includes/usage-examples/write-code-examples.php new file mode 100644 index 00000000..8e508822 --- /dev/null +++ b/source/includes/usage-examples/write-code-examples.php @@ -0,0 +1,116 @@ +db->coll; + +// Inserts one document that stores the specified values +// start-insert-one +$result = $collection->insertOne([ + '' => '', + '' => '', +]); +// end-insert-one + +// Inserts multiple documents that store the specified values +// start-insert-multiple +$result = $collection->insertMany( + [ + '' => '', + '' => '', + ], + [ + '' => '', + '' => '', + ], +); +// end-insert-multiple + +// Updates a document that matches the specified criteria +// start-update-one +$result = $collection->updateOne( + ['' => ''], + ['$set' => ['' => '']], +); +// end-update-one + +// Updates all documents that match the specified criteria +// start-update-multiple +$result = $collection->updateMany( + ['' => ''], + ['$set' => ['' => '']], +); +// end-update-multiple + +// start-replace-one +$result = $collection->replaceOne( + ['' => ''], + [ + '' => '', + '' => '', + ], +); +// end-replace-one + +// Deletes a document that matches the specified criteria +// start-delete-one +$result = $collection->deleteOne(['' => '']); +// end-delete-one + +// Deletes all documents that match the specified criteria +// start-delete-multiple +$result = $collection->deleteMany(['' => '']); +// end-delete-multiple + +// Runs a bulk operation based on the instructions in each array entry +// start-bulk-write +$result = $collection->bulkWrite( + [ + [ + 'insertOne' => [ + ['' => ''], + ], + ], + [ + 'replaceOne' => [ + ['' => ''], + [ + '' => '', + '' => '', + ], + ], + ], + [ + 'updateOne' => [ + ['' => ''], + ['$set' => ['' => '']], + ], + ], + [ + 'updateMany' => [ + ['' => ''], + ['$set' => ['' => '']], + ], + ], + [ + 'deleteOne' => [ + ['' => ''], + ], + ], + [ + 'deleteMany' => [ + ['' => ''], + ], + ], + ] +); +// end-bulk-write + +// Stores a file in a GridFS bucket and writes data to the file +// start-gridfs-upload +$bucket = $client->selectDatabase('')->selectGridFSBucket(); +$stream = $bucket->openUploadStream(''); +fwrite($stream, ''); +fclose($stream); +// end-gridfs-upload diff --git a/source/write.txt b/source/write.txt index 6d95c80a..f2cd201e 100644 --- a/source/write.txt +++ b/source/write.txt @@ -4,6 +4,20 @@ Write Data to MongoDB ===================== +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :description: Learn how to use the PHP Library to write data to MongoDB. + :keywords: usage examples, save, crud, create, code example + .. toctree:: :titlesonly: :maxdepth: 1 @@ -12,3 +26,161 @@ Write Data to MongoDB /write/replace /write/insert /write/update + +Overview +-------- + +On this page, you can see copyable code examples that show common +{+php-library+} methods for writing data to MongoDB. + +.. tip:: + + To learn more about any of the methods shown on this page, see the link + provided in each section. + +To use an example from this page, copy the code example into the +:ref:`sample application ` or your own application. +Make sure to set the ``MONGODB_URI`` environment variable to the +connection string for your MongoDB deployment, and replace the +```` and ```` placeholders with values for your +target namespace. + +.. _php-write-sample: + +.. include:: /includes/usage-examples/sample-app-intro.rst + +.. literalinclude:: /includes/usage-examples/sample-app.php + :language: php + :dedent: + :linenos: + :emphasize-lines: 10-12 + +Insert One +---------- + +The following code shows how to insert a single document into a collection: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-insert-one + :end-before: end-insert-one + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::insertOne()`` method, see the +:ref:`Insert Documents ` guide. + +Insert Multiple +--------------- + +The following code shows how to insert multiple documents into a collection: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-insert-multiple + :end-before: end-insert-multiple + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::insertMany()`` method, see the +:ref:`Insert Documents ` guide. + +Update One +---------- + +The following code shows how to update a single document in a collection by creating +or editing a field: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-update-one + :end-before: end-update-one + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::updateOne()`` method, see the +:ref:`Update Documents ` guide. + +Update Multiple +--------------- + +The following code shows how to update multiple documents in a collection by creating +or editing a field: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-update-multiple + :end-before: end-update-multiple + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::updateMany()`` method, see the +:ref:`Update Documents ` guide. + +Replace One +----------- + +The following code shows how to replace a single document in a collection +with another document: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-replace-one + :end-before: end-replace-one + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::replaceOne()`` method, see the +:ref:`Replace Documents ` guide. + +Delete One +---------- + +The following code shows how to delete a single document in a collection: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-delete-one + :end-before: end-delete-one + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::deleteOne()`` method, see the +:ref:`Delete Documents ` guide. + +Delete Multiple +--------------- + +The following code shows how to delete multiple documents in a collection: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-delete-multiple + :end-before: end-delete-multiple + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::deleteMany()`` method, see the +:ref:`Delete Documents ` guide. + +Bulk Write +---------- + +The following code shows how to perform multiple write operations in a single bulk +operation: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-bulk-write + :end-before: end-bulk-write + :language: php + :dedent: + +To learn more about the ``MongoDB\Collection::bulkWrite()`` method, see the +:ref:`Bulk Write ` guide. + +Store Large Files +----------------- + +The following code shows how to store files in a GridFS bucket by +creating an upload stream: + +.. literalinclude:: /includes/usage-examples/write-code-examples.php + :start-after: start-gridfs-upload + :end-before: end-gridfs-upload + :language: php + :dedent: + +To learn more about GridFS, see the :ref:`Store Large Files ` guide.