Skip to content

Commit c5380ec

Browse files
* converted code-blocks to io-code-blocks
1 parent da416cc commit c5380ec

File tree

17 files changed

+1030
-516
lines changed

17 files changed

+1030
-516
lines changed

source/fundamentals/bson.txt

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,25 @@ The following example demonstrates how you can use the ``Decode()``
237237
method to unmarshal and read the result of a simple ``FindOne()``
238238
operation:
239239

240-
.. code-block:: go
240+
.. io-code-block::
241+
:copyable: true
241242

242-
coll := client.Database("school").Collection("students")
243-
filter := bson.D{{"age", 8}}
243+
.. input::
244+
:language: go
244245

245-
var result bson.D
246-
err := coll.FindOne(context.TODO(), filter).Decode(&result)
247-
...
246+
coll := client.Database("school").Collection("students")
247+
filter := bson.D{{"age", 8}}
248248

249-
fmt.Println(result)
249+
var result bson.D
250+
err := coll.FindOne(context.TODO(), filter).Decode(&result)
251+
...
250252

251-
The output of the preceding code should look like this:
253+
fmt.Println(result)
252254

253-
.. code-block:: none
255+
.. output::
256+
:language: none
254257

255-
[{_id ObjectID("...")} {first_name Arthur} {street 1 Fern Way} {city Elwood City} {state PA} {age 8}]
258+
[{_id ObjectID("...")} {first_name Arthur} {street 1 Fern Way} {city Elwood City} {state PA} {age 8}]
256259

257260
The ``Cursor`` type also uses the ``All()`` method, which unmarshals all
258261
documents stored in the cursor into an array at the same time.
@@ -264,27 +267,30 @@ of ``[]byte`` type.
264267
The following code demonstrates how you can unmarshal BSON back into a
265268
user-defined struct by using methods from the ``bson`` package:
266269

267-
.. code-block:: go
270+
.. io-code-block::
271+
:copyable: true
268272

269-
type Item struct {
270-
Category string
271-
Quantity int32
272-
}
273-
...
273+
.. input::
274+
:language: go
274275

275-
doc, err := bson.Marshal(bson.D{{"category", "plate"}, {"quantity", 6}})
276-
...
277-
var test Item
278-
err = bson.Unmarshal(doc, &test)
279-
...
280-
fmt.Printf("Unmarshalled Struct:\n%+v\n", test)
276+
type Item struct {
277+
Category string
278+
Quantity int32
279+
}
280+
...
281281

282-
The output of the preceding code should look like this:
282+
doc, err := bson.Marshal(bson.D{{"category", "plate"}, {"quantity", 6}})
283+
...
284+
var test Item
285+
err = bson.Unmarshal(doc, &test)
286+
...
287+
fmt.Printf("Unmarshalled Struct:\n%+v\n", test)
283288

284-
.. code-block:: none
289+
.. output::
290+
:language: none
285291

286-
Unmarshalled Struct:
287-
{Category:plate Quantity:6}
292+
Unmarshalled Struct:
293+
{Category:plate Quantity:6}
288294

289295
.. note::
290296

source/fundamentals/crud/compound-operations.txt

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,26 @@ Example
107107
The following example matches and deletes a document where the ``type``
108108
is "Assam" with the ``FindOneAndDelete()`` method:
109109

110-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
111-
:language: go
112-
:dedent:
113-
:start-after: begin FindOneAndDelete
114-
:end-before: end FindOneAndDelete
110+
.. io-code-block::
111+
:copyable: true
112+
113+
.. input::
114+
:language: go
115+
116+
filter := bson.D{{"type", "Assam"}}
115117

116-
After running this example, the output resembles the following:
118+
var deletedDoc bson.D
119+
err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
120+
if err != nil {
121+
panic(err)
122+
}
117123

118-
.. code-block:: none
119-
:copyable: false
124+
fmt.Println(deletedDoc)
120125

121-
[{_id ObjectID("...")} {type Assam} {rating 5}]
126+
.. output::
127+
:language: none
128+
129+
[{_id ObjectID("...")} {type Assam} {rating 5}]
122130

123131
.. _golang-find-and-update:
124132

@@ -201,18 +209,28 @@ The following example performs the following actions in order with the
201209
- Updates the matched document's ``rating`` to ``9``
202210
- Returns the updated document
203211

204-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
205-
:language: go
206-
:dedent:
207-
:start-after: begin FindOneAndUpdate
208-
:end-before: end FindOneAndUpdate
212+
.. io-code-block::
213+
:copyable: true
209214

210-
After running this example, the output resembles the following:
215+
.. input::
216+
:language: go
211217

212-
.. code-block:: none
213-
:copyable: false
218+
filter := bson.D{{"type", "Oolong"}}
219+
update := bson.D{{"$set", bson.D{{"rating", 9}}}}
220+
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
214221

215-
[{_id ObjectID("...")} {type Oolong} {rating 9}]
222+
var updatedDoc bson.D
223+
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
224+
if err != nil {
225+
panic(err)
226+
}
227+
228+
fmt.Println(updatedDoc)
229+
230+
.. output::
231+
:language: none
232+
233+
[{_id ObjectID("...")} {type Oolong} {rating 9}]
216234

217235
.. _golang-find-and-replace:
218236

@@ -290,18 +308,27 @@ The following example performs the following actions in order with the
290308
- Matches a document where the ``type`` is "English Breakfast"
291309
- Replaces the matched document with a new document where the ``type`` is "Ceylon" and ``rating`` is ``6``
292310

293-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/compoundOperations.go
294-
:language: go
295-
:dedent:
296-
:start-after: begin FindOneAndReplace
297-
:end-before: end FindOneAndReplace
311+
.. io-code-block::
312+
:copyable: true
313+
314+
.. input::
315+
:language: go
316+
317+
filter := bson.D{{"type", "English Breakfast"}}
318+
replacement := bson.D{{"type", "Ceylon"}, {"rating", 6}}
298319

299-
After running this example, the output resembles the following:
320+
var previousDoc bson.D
321+
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
322+
if err != nil {
323+
panic(err)
324+
}
300325

301-
.. code-block:: none
302-
:copyable: false
326+
fmt.Println(previousDoc)
327+
328+
.. output::
329+
:language: none
303330

304-
[{_id ObjectID("...")} {type English Breakfast} {rating 5}]
331+
[{_id ObjectID("...")} {type English Breakfast} {rating 5}]
305332

306333
Additional Information
307334
----------------------

source/fundamentals/crud/read-operations/count.txt

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,24 @@ Example
9292
The following example counts the number of documents where the
9393
``rating`` is less than ``6``:
9494

95-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go
96-
:language: go
97-
:dedent:
98-
:start-after: begin count documents
99-
:end-before: end count documents
95+
.. io-code-block::
96+
:copyable: true
97+
98+
.. input::
99+
:language: go
100100

101-
After running this example, the output resembles the following:
101+
filter := bson.D{{"rating", bson.D{{"$lt", 6}}}}
102102

103-
.. code-block:: none
104-
:copyable: false
103+
count, err := coll.CountDocuments(context.TODO(), filter)
104+
if err != nil {
105+
panic(err)
106+
}
107+
fmt.Printf("Number of ratings less than six: %d\n", count)
105108

106-
Number of ratings less than six: 4
109+
.. output::
110+
:language: none
111+
112+
Number of ratings less than six: 4
107113

108114
Aggregation
109115
-----------
@@ -119,18 +125,32 @@ The following example performs the following actions:
119125
- Counts the number of documents where the ``rating`` is greater than ``5``
120126
- Assigns the count to a field called ``total_documents``
121127

122-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go
123-
:language: go
124-
:dedent:
125-
:start-after: begin aggregate count
126-
:end-before: end aggregate count
128+
.. io-code-block::
129+
:copyable: true
127130

128-
After running this example, the output resembles the following:
131+
.. input::
132+
:language: go
129133

130-
.. code-block:: none
131-
:copyable: false
134+
matchStage := bson.D{{"$match", bson.D{{"rating", bson.D{{"$gt", 5}}}}}}
135+
countStage := bson.D{{"$count", "total_documents"}}
132136

133-
[{total_documents 5}]
137+
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, countStage})
138+
if err != nil {
139+
panic(err)
140+
}
141+
142+
var results []bson.D
143+
if err = cursor.All(context.TODO(), &results); err != nil {
144+
panic(err)
145+
}
146+
for _, result := range results {
147+
fmt.Println(result)
148+
}
149+
150+
.. output::
151+
:language: none
152+
153+
[{total_documents 5}]
134154

135155
.. _golang-estimated-count:
136156

@@ -173,18 +193,22 @@ Example
173193
The following example estimates the number of documents in the
174194
``ratings`` collection:
175195

176-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go
177-
:language: go
178-
:dedent:
179-
:start-after: begin est doc count
180-
:end-before: end est doc count
196+
.. io-code-block::
197+
:copyable: true
198+
199+
.. input::
200+
:language: go
181201

182-
After running this example, the output resembles the following:
202+
count, err := coll.EstimatedDocumentCount(context.TODO())
203+
if err != nil {
204+
panic(err)
205+
}
206+
fmt.Printf("Estimated number of documents in the ratings collection: %d\n", count)
183207

184-
.. code-block:: none
185-
:copyable: false
208+
.. output::
209+
:language: none
186210

187-
Estimated number of documents in the ratings collection: 9
211+
Estimated number of documents in the ratings collection: 9
188212

189213
Additional Information
190214
----------------------

source/fundamentals/crud/read-operations/distinct.txt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,28 @@ Example
8383
The following example matches all documents and prints the distinct values
8484
of the ``type`` field using the ``Distinct()`` method:
8585

86-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/distinctValues.go
87-
:language: go
88-
:dedent:
89-
:start-after: begin distinct
90-
:end-before: end distinct
91-
92-
After running this example, the output resembles the following:
93-
94-
.. code-block:: none
95-
:copyable: false
96-
97-
Earl Grey
98-
Masala
99-
Matcha
100-
Oolong
86+
.. io-code-block::
87+
:copyable: true
88+
89+
.. input::
90+
:language: go
91+
92+
results, err := coll.Distinct(context.TODO(), "type", bson.D{})
93+
if err != nil {
94+
panic(err)
95+
}
96+
97+
for _, result := range results {
98+
fmt.Println(result)
99+
}
100+
101+
.. output::
102+
:language: none
103+
104+
Earl Grey
105+
Masala
106+
Matcha
107+
Oolong
101108

102109
Additional Information
103110
----------------------

0 commit comments

Comments
 (0)