diff --git a/source/includes/usage-examples/sample-app-intro.rst b/source/includes/usage-examples/sample-app-intro.rst new file mode 100644 index 00000000..c0f95c6a --- /dev/null +++ b/source/includes/usage-examples/sample-app-intro.rst @@ -0,0 +1,9 @@ +Sample Application +~~~~~~~~~~~~~~~~~~ + +You can use the following sample application to test the code examples on this +page. To use the sample application, perform the following steps: + +1. Ensure you have the {+driver-short+} installed in your {+language+} project. +#. Copy the following code and paste it into a new ``.rb`` file. +#. Copy a code example from this page and paste it on the specified lines in the file. \ No newline at end of file diff --git a/source/includes/usage-examples/sample-app.rb b/source/includes/usage-examples/sample-app.rb new file mode 100644 index 00000000..fb00e456 --- /dev/null +++ b/source/includes/usage-examples/sample-app.rb @@ -0,0 +1,16 @@ +require 'bundler/inline' +gemfile do + source 'https://rubygems.org' + gem 'mongo' +end + +uri = "" + +Mongo::Client.new(uri) do |client| + database = client.use('') + collection = database[:] + + # Start example code here + + # End example code here +end \ No newline at end of file diff --git a/source/includes/usage-examples/write-code-examples.rb b/source/includes/usage-examples/write-code-examples.rb new file mode 100644 index 00000000..261e2568 --- /dev/null +++ b/source/includes/usage-examples/write-code-examples.rb @@ -0,0 +1,62 @@ +require 'bundler/inline' +gemfile do + source 'https://rubygems.org' + gem 'mongo' +end + +uri = "" + +Mongo::Client.new(uri) do |client| + database = client.use('sample_restaurants') + collection = database[:restaurants] + + # start-insert-one + document = { field_name: '' } + collection.insert_one(document) + # end-insert-one + + # start-insert-many + documents = [ + { field_name: '' }, + { field_name: '' } + ] + collection.insert_many(documents) + # end-insert-many + + # start-update-one + filter = { field_name: '' } + update = { } + collection.update_one(filter, update) + # end-update-one + + # start-update-many + filter = { field_name: '' } + update = { } + collection.update_many(filter, update) + # end-update-many + + # start-replace-one + filter = { field_name: '' } + new_document = { field_name: '' } + collection.replace_one(filter, new_document) + # end-replace-one + + # start-delete-one + filter = { field_name: '' } + collection.delete_one(filter) + # end-delete-one + + # start-delete-many + filter = { field_name: '' } + collection.delete_many(filter) + # end-delete-many + + # start-bulk-write + operations = [ + { }, + { }, + { }, + ] + collection.bulk_write(operations) + # end-bulk-write +end \ No newline at end of file diff --git a/source/includes/write/delete.rb b/source/includes/write/delete.rb index 368dc9d7..f9b05eb3 100644 --- a/source/includes/write/delete.rb +++ b/source/includes/write/delete.rb @@ -4,7 +4,7 @@ gem 'mongo' end -uri = "" +uri = "" Mongo::Client.new(uri) do |client| # start-db-coll diff --git a/source/includes/write/insert.rb b/source/includes/write/insert.rb index 818f3121..a9ea7835 100644 --- a/source/includes/write/insert.rb +++ b/source/includes/write/insert.rb @@ -4,7 +4,7 @@ gem 'mongo' end -uri = "" +uri = "" Mongo::Client.new(uri) do |client| diff --git a/source/includes/write/update.rb b/source/includes/write/update.rb index 392a5d27..5b11924b 100644 --- a/source/includes/write/update.rb +++ b/source/includes/write/update.rb @@ -4,7 +4,7 @@ gem 'mongo' end -uri = "" +uri = "" Mongo::Client.new(uri) do |client| # start-db-coll diff --git a/source/write.txt b/source/write.txt index 5daa7c63..d6717b54 100644 --- a/source/write.txt +++ b/source/write.txt @@ -30,4 +30,152 @@ Write Data to MongoDB /write/transactions /write/gridfs -.. TODO \ No newline at end of file +Overview +-------- + +On this page, you can see copyable code examples that show common methods you +can use to write data to MongoDB by using the {+driver-short+}. + +.. 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. Be sure to replace all +placeholders in the code examples, such as ````, with the +relevant values for your MongoDB deployment. + +.. _ruby-write-sample: + +.. include:: /includes/usage-examples/sample-app-intro.rst + +.. literalinclude:: /includes/usage-examples/sample-app.rb + :language: ruby + :copyable: + :linenos: + :emphasize-lines: 13-15 + +Insert One +---------- + +The following code shows how to insert a single document into a collection: + +.. literalinclude:: /includes/usage-examples/write-code-examples.rb + :start-after: start-insert-one + :end-before: end-insert-one + :language: ruby + :copyable: + :dedent: + +To learn more about the ``insert_one`` 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.rb + :start-after: start-insert-many + :end-before: end-insert-many + :language: ruby + :copyable: + :dedent: + +To learn more about the ``insert_many`` 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.rb + :start-after: start-update-one + :end-before: end-update-one + :language: ruby + :copyable: + :dedent: + +To learn more about the ``update_one`` method, see the +:ref:`ruby-write-update` 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.rb + :start-after: start-update-many + :end-before: end-update-many + :language: ruby + :copyable: + :dedent: + +To learn more about the ``update_many`` method, see the +:ref:`ruby-write-update` guide. + +Replace One +----------- + +The following code shows how to replace a single document in a collection with a new +document: + +.. literalinclude:: /includes/usage-examples/write-code-examples.rb + :start-after: start-replace-one + :end-before: end-replace-one + :language: ruby + :copyable: + :dedent: + +To learn more about the ``replace_one`` 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.rb + :start-after: start-delete-one + :end-before: end-delete-one + :language: ruby + :copyable: + :dedent: + +To learn more about the ``delete_one`` 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.rb + :start-after: start-delete-many + :end-before: end-delete-many + :language: ruby + :copyable: + :dedent: + +To learn more about the ``delete_many`` 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.rb + :start-after: start-bulk-write + :end-before: end-bulk-write + :language: ruby + :copyable: + :dedent: + +To learn more about the ``bulk_write`` +method, see the :ref:`Bulk Write Operations ` guide.