Skip to content

Commit 14e1d51

Browse files
authored
Merge pull request #114 from mcmorisi/DOCSP-45185-write
DOCSP-45185: Write landing page
2 parents 8a2d3e9 + 43c4f29 commit 14e1d51

File tree

7 files changed

+239
-4
lines changed

7 files changed

+239
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Sample Application
2+
~~~~~~~~~~~~~~~~~~
3+
4+
You can use the following sample application to test the code examples on this
5+
page. To use the sample application, perform the following steps:
6+
7+
1. Ensure you have the {+driver-short+} installed in your {+language+} project.
8+
#. Copy the following code and paste it into a new ``.rb`` file.
9+
#. Copy a code example from this page and paste it on the specified lines in the file.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'bundler/inline'
2+
gemfile do
3+
source 'https://rubygems.org'
4+
gem 'mongo'
5+
end
6+
7+
uri = "<connection string>"
8+
9+
Mongo::Client.new(uri) do |client|
10+
database = client.use('<database name>')
11+
collection = database[:<collection name>]
12+
13+
# Start example code here
14+
15+
# End example code here
16+
end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require 'bundler/inline'
2+
gemfile do
3+
source 'https://rubygems.org'
4+
gem 'mongo'
5+
end
6+
7+
uri = "<connection string>"
8+
9+
Mongo::Client.new(uri) do |client|
10+
database = client.use('sample_restaurants')
11+
collection = database[:restaurants]
12+
13+
# start-insert-one
14+
document = { field_name: '<field value>' }
15+
collection.insert_one(document)
16+
# end-insert-one
17+
18+
# start-insert-many
19+
documents = [
20+
{ field_name: '<field value 1>' },
21+
{ field_name: '<field value 2>' }
22+
]
23+
collection.insert_many(documents)
24+
# end-insert-many
25+
26+
# start-update-one
27+
filter = { field_name: '<field value>' }
28+
update = { <update definition> }
29+
collection.update_one(filter, update)
30+
# end-update-one
31+
32+
# start-update-many
33+
filter = { field_name: '<field value>' }
34+
update = { <update definition> }
35+
collection.update_many(filter, update)
36+
# end-update-many
37+
38+
# start-replace-one
39+
filter = { field_name: '<field value>' }
40+
new_document = { field_name: '<field value>' }
41+
collection.replace_one(filter, new_document)
42+
# end-replace-one
43+
44+
# start-delete-one
45+
filter = { field_name: '<field value>' }
46+
collection.delete_one(filter)
47+
# end-delete-one
48+
49+
# start-delete-many
50+
filter = { field_name: '<field value>' }
51+
collection.delete_many(filter)
52+
# end-delete-many
53+
54+
# start-bulk-write
55+
operations = [
56+
{ <bulk operation 1> },
57+
{ <bulk operation 2> },
58+
{ <bulk operation 3> },
59+
]
60+
collection.bulk_write(operations)
61+
# end-bulk-write
62+
end

source/includes/write/delete.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
gem 'mongo'
55
end
66

7-
uri = "<connection string URI>"
7+
uri = "<connection string>"
88

99
Mongo::Client.new(uri) do |client|
1010
# start-db-coll

source/includes/write/insert.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
gem 'mongo'
55
end
66

7-
uri = "<connection string URI>"
7+
uri = "<connection string>"
88

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

source/includes/write/update.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
gem 'mongo'
55
end
66

7-
uri = "<connection string URI>"
7+
uri = "<connection string>"
88

99
Mongo::Client.new(uri) do |client|
1010
# start-db-coll

source/write.txt

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,152 @@ Write Data to MongoDB
3030
/write/transactions
3131
/write/gridfs
3232

33-
.. TODO
33+
Overview
34+
--------
35+
36+
On this page, you can see copyable code examples that show common methods you
37+
can use to write data to MongoDB by using the {+driver-short+}.
38+
39+
.. tip::
40+
41+
To learn more about any of the methods shown on this page, see the link
42+
provided in each section.
43+
44+
To use an example from this page, copy the code example into the :ref:`sample
45+
application <ruby-write-sample>` or your own application. Be sure to replace all
46+
placeholders in the code examples, such as ``<connection string>``, with the
47+
relevant values for your MongoDB deployment.
48+
49+
.. _ruby-write-sample:
50+
51+
.. include:: /includes/usage-examples/sample-app-intro.rst
52+
53+
.. literalinclude:: /includes/usage-examples/sample-app.rb
54+
:language: ruby
55+
:copyable:
56+
:linenos:
57+
:emphasize-lines: 13-15
58+
59+
Insert One
60+
----------
61+
62+
The following code shows how to insert a single document into a collection:
63+
64+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
65+
:start-after: start-insert-one
66+
:end-before: end-insert-one
67+
:language: ruby
68+
:copyable:
69+
:dedent:
70+
71+
To learn more about the ``insert_one`` method, see the
72+
:ref:`Insert Documents <ruby-write-insert>` guide.
73+
74+
Insert Multiple
75+
---------------
76+
77+
The following code shows how to insert multiple documents into a collection:
78+
79+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
80+
:start-after: start-insert-many
81+
:end-before: end-insert-many
82+
:language: ruby
83+
:copyable:
84+
:dedent:
85+
86+
To learn more about the ``insert_many`` method, see the
87+
:ref:`Insert Documents <ruby-write-insert>` guide.
88+
89+
Update One
90+
----------
91+
92+
The following code shows how to update a single document in a collection by
93+
creating or editing a field:
94+
95+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
96+
:start-after: start-update-one
97+
:end-before: end-update-one
98+
:language: ruby
99+
:copyable:
100+
:dedent:
101+
102+
To learn more about the ``update_one`` method, see the
103+
:ref:`ruby-write-update` guide.
104+
105+
Update Multiple
106+
---------------
107+
108+
The following code shows how to update multiple documents in a collection by
109+
creating or editing a field:
110+
111+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
112+
:start-after: start-update-many
113+
:end-before: end-update-many
114+
:language: ruby
115+
:copyable:
116+
:dedent:
117+
118+
To learn more about the ``update_many`` method, see the
119+
:ref:`ruby-write-update` guide.
120+
121+
Replace One
122+
-----------
123+
124+
The following code shows how to replace a single document in a collection with a new
125+
document:
126+
127+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
128+
:start-after: start-replace-one
129+
:end-before: end-replace-one
130+
:language: ruby
131+
:copyable:
132+
:dedent:
133+
134+
To learn more about the ``replace_one`` method, see the
135+
:ref:`Replace Documents <ruby-write-replace>` guide.
136+
137+
Delete One
138+
----------
139+
140+
The following code shows how to delete a single document in a collection:
141+
142+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
143+
:start-after: start-delete-one
144+
:end-before: end-delete-one
145+
:language: ruby
146+
:copyable:
147+
:dedent:
148+
149+
To learn more about the ``delete_one`` method, see the
150+
:ref:`Delete Documents <ruby-write-delete>` guide.
151+
152+
Delete Multiple
153+
---------------
154+
155+
The following code shows how to delete multiple documents in a collection:
156+
157+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
158+
:start-after: start-delete-many
159+
:end-before: end-delete-many
160+
:language: ruby
161+
:copyable:
162+
:dedent:
163+
164+
To learn more about the ``delete_many`` method, see the
165+
:ref:`Delete Documents <ruby-write-delete>` guide.
166+
167+
Bulk Write
168+
----------
169+
170+
The following code shows how to perform multiple write operations in a single
171+
bulk operation:
172+
173+
.. literalinclude:: /includes/usage-examples/write-code-examples.rb
174+
:start-after: start-bulk-write
175+
:end-before: end-bulk-write
176+
:language: ruby
177+
:copyable:
178+
:dedent:
179+
180+
To learn more about the ``bulk_write``
181+
method, see the :ref:`Bulk Write Operations <ruby-bulk-write>` guide.

0 commit comments

Comments
 (0)