Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions source/includes/usage-examples/sample-app-intro.rst
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions source/includes/usage-examples/sample-app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mongo'
end

uri = "<connection string>"

Mongo::Client.new(uri) do |client|
database = client.use('<database name>')
collection = database[:<collection name>]

# Start example code here

# End example code here
end
62 changes: 62 additions & 0 deletions source/includes/usage-examples/write-code-examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'mongo'
end

uri = "<connection string>"

Mongo::Client.new(uri) do |client|
database = client.use('sample_restaurants')
collection = database[:restaurants]

# start-insert-one
document = { field_name: '<field value>' }
collection.insert_one(document)
# end-insert-one

# start-insert-many
documents = [
{ field_name: '<field value 1>' },
{ field_name: '<field value 2>' }
]
collection.insert_many(documents)
# end-insert-many

# start-update-one
filter = { field_name: '<field value>' }
update = { <update definition> }
collection.update_one(filter, update)
# end-update-one

# start-update-many
filter = { field_name: '<field value>' }
update = { <update definition> }
collection.update_many(filter, update)
# end-update-many

# start-replace-one
filter = { field_name: '<field value>' }
new_document = { field_name: '<field value>' }
collection.replace_one(filter, new_document)
# end-replace-one

# start-delete-one
filter = { field_name: '<field value>' }
collection.delete_one(filter)
# end-delete-one

# start-delete-many
filter = { field_name: '<field value>' }
collection.delete_many(filter)
# end-delete-many

# start-bulk-write
operations = [
{ <bulk operation 1> },
{ <bulk operation 2> },
{ <bulk operation 3> },
]
collection.bulk_write(operations)
# end-bulk-write
end
2 changes: 1 addition & 1 deletion source/includes/write/delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
gem 'mongo'
end

uri = "<connection string URI>"
uri = "<connection string>"

Mongo::Client.new(uri) do |client|
# start-db-coll
Expand Down
2 changes: 1 addition & 1 deletion source/includes/write/insert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
gem 'mongo'
end

uri = "<connection string URI>"
uri = "<connection string>"

Mongo::Client.new(uri) do |client|

Expand Down
2 changes: 1 addition & 1 deletion source/includes/write/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
gem 'mongo'
end

uri = "<connection string URI>"
uri = "<connection string>"

Mongo::Client.new(uri) do |client|
# start-db-coll
Expand Down
150 changes: 149 additions & 1 deletion source/write.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,152 @@ Write Data to MongoDB
/write/transactions
/write/gridfs

.. TODO
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 <ruby-write-sample>` or your own application. Be sure to replace all
placeholders in the code examples, such as ``<connection string>``, 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 <ruby-write-insert>` 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 <ruby-write-insert>` 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 <ruby-write-replace>` 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 <ruby-write-delete>` 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 <ruby-write-delete>` 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 <ruby-bulk-write>` guide.
Loading