Skip to content

Commit 05d33c0

Browse files
authored
DOCSP-27039: Fundamentals cleanup (#316)
* DOCSP-27039: Fundamentals cleanup * change streams, other fixes * unmarshalling method * monospace
1 parent 2283563 commit 05d33c0

35 files changed

+372
-261
lines changed

source/fundamentals/aggregation.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Examples
8484
--------
8585

8686
The examples in this section use the following ``Tea`` struct as a model for documents
87-
in the ``menu`` collection:
87+
in the ``tea`` collection:
8888

8989
.. literalinclude:: /includes/fundamentals/code-snippets/aggregation.go
9090
:start-after: start-tea-struct
@@ -93,16 +93,16 @@ in the ``menu`` collection:
9393
:dedent:
9494

9595
To run the examples in this section, load the sample data into the
96-
``tea.menu`` collection with the following snippet:
96+
``tea`` collection in the ``db`` database with the following snippet:
9797

9898
.. literalinclude:: /includes/fundamentals/code-snippets/aggregation.go
9999
:start-after: begin insert docs
100100
:end-before: end insert docs
101101
:language: go
102102
:dedent:
103103

104-
Each document represents a tea on the menu of a shop and contains
105-
information about the tea type, the available toppings, and the price.
104+
Each document contains information about the tea type, the available toppings, and
105+
the price.
106106

107107
Average Rating
108108
~~~~~~~~~~~~~~

source/fundamentals/bson.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ use in the {+driver-short+}:
7878
corresponding to the field type.
7979

8080
* - ``minsize``
81-
- If the field type is type int64, uint, uint32, or uint64 and the value of
82-
the field can fit in a signed int32, the field will be serialized
83-
as a BSON int32 rather than a BSON int64. If the value can't fit
84-
in a signed int32, this tag is ignored.
81+
- If the field is type ``int64``, ``uint``, ``uint32``, or ``uint64`` and
82+
the value of the field can fit in a signed ``int32``, the field will be
83+
serialized as a BSON ``int32`` rather than a BSON ``int64``. If the value
84+
can't fit in a signed ``int32``, this tag is ignored.
8585

8686
* - ``truncate``
8787
- If the field type is a non-float numeric type, BSON doubles
@@ -134,7 +134,7 @@ the following rules:
134134
Age int
135135
}
136136

137-
coll := client.Database("school").Collection("students")
137+
coll := client.Database("db").Collection("students")
138138
address1 := Address{ "1 Lakewood Way", "Elwood City", "PA" }
139139
student1 := Student{ FirstName : "Arthur", Address : address1, Age : 8}
140140
_, err = coll.InsertOne(context.TODO(), student1)
@@ -183,7 +183,7 @@ the following rules:
183183
Age int
184184
}
185185

186-
coll := client.Database("school").Collection("students")
186+
coll := client.Database("db").Collection("students")
187187
address1 := Address{ "1 Lakewood Way", "Elwood City", "PA" }
188188
student1 := Student{ FirstName : "Arthur", Address : address1, Age : 8}
189189
_, err = coll.InsertOne(context.TODO(), student1)
@@ -285,7 +285,7 @@ operation:
285285
.. input::
286286
:language: go
287287

288-
coll := client.Database("school").Collection("students")
288+
coll := client.Database("db").Collection("students")
289289
filter := bson.D{{"age", 8}}
290290

291291
var result bson.D

source/fundamentals/collations.txt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,19 @@ that alphabetically precedes ``"Infinite Jest"``:
135135
if err = cursor.All(context.TODO(), &results); err != nil {
136136
panic(err)
137137
}
138+
138139
for _, result := range results {
139-
fmt.Println(result)
140-
}
140+
res, _ := bson.MarshalExtJSON(result, false, false)
141+
fmt.Println(string(res))
142+
}
141143

142144
.. output::
143145
:language: none
144146
:visible: false
145147

146-
[{name Ça} {length 1138}]
147-
[{name Cryptonomicon} {length 918}]
148-
[{name Emma} {length 474}]
148+
{"name":"Emma","length":"474"}
149+
{"name":"Cryptonomicon","length":"918"}
150+
{"name":"Ça","length":"1138"}
149151

150152
Without specifying a default ``books`` collation, the ``Find()`` method would follow default
151153
binary collation rules to determine the ``name`` values that precede ``"Infinite Jest"``. These
@@ -155,8 +157,8 @@ the following:
155157
.. code-block:: json
156158
:copyable: false
157159

158-
[{name Cryptonomicon} {length 918}]
159-
[{name Emma} {length 474}]
160+
{"name":"Emma","length":"474"}
161+
{"name":"Cryptonomicon","length":"918"}
160162

161163
To learn more about the ``Find()`` method, see :ref:`golang-retrieve`.
162164

@@ -258,27 +260,30 @@ order:
258260
}
259261

260262
for _, result := range results {
261-
fmt.Println(result)
262-
}
263+
res, _ := bson.MarshalExtJSON(result, false, false)
264+
fmt.Println(string(res))
265+
}
263266

264267
.. output::
265268
:language: none
266269
:visible: false
267270

268-
[{name Infinite Jest} {length 1104}]
269-
[{name Ça} {length 1138}]
270-
[{name Les Misérables} {length 1462}]
271+
{"name":"Les Misérables","length":"1462"}
272+
{"name":"Infinite Jest","length":"1104"}
273+
{"name":"Ça","length":"1138"}
271274

272275
Without specifying a collation with a ``NumericOrdering`` field set to ``true``, the
273-
same ``Find()`` operation would compare ``length`` values as strings. For example, the
274-
operation would consider the string ``"824"`` as greater than ``"1000"``. The
275-
output would resemble the following:
276+
same ``Find()`` operation compares ``length`` values as strings. In this case, the
277+
output resembles the following:
276278

277279
.. code-block:: json
278280
:copyable: false
279281

280-
[{name Emma} {length 474}]
281-
[{name Cryptonomicon} {length 918}]
282+
{"name":"Emma","length":"474"}
283+
{"name":"Les Misérables","length":"1462"}
284+
{""name":"Infinite Jest","length":"1104"}
285+
{"name":"Cryptonomicon","length":"918"}
286+
{"name":"Ça","length":"1138"}
282287

283288
Additional Information
284289
----------------------

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

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
Monitor Data Changes
66
====================
77

8+
.. facet::
9+
:name: genre
10+
:values: reference
11+
812
.. meta::
9-
:description: Learn about opening change streams and monitoring data changes in MongoDB by using the {+driver-long+}.
13+
:keywords: code example, delta
1014

1115
.. contents:: On this page
1216
:local:
@@ -17,19 +21,27 @@ Monitor Data Changes
1721
Overview
1822
--------
1923

20-
In this guide, you can learn how to monitor document changes with a change stream.
24+
In this guide, you can learn how to monitor document changes by using a **change stream**.
2125

2226
A change stream outputs new change events, providing access to real-time data changes.
2327
You can open a change stream on a collection, database, or client object.
2428

2529
Sample Data
2630
~~~~~~~~~~~
2731

32+
The examples in this guide use the following ``Course`` struct as a model for documents
33+
in the ``courses`` collection:
34+
35+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
36+
:start-after: begin struct
37+
:end-before: end struct
38+
:language: go
39+
:dedent:
40+
2841
To run the examples in this guide, load these documents into the
29-
``db.courses`` collection with the following
30-
snippet:
42+
``courses`` collection in the ``db`` database with the following snippet:
3143

32-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort.go
44+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
3345
:language: go
3446
:dedent:
3547
:start-after: begin insertDocs
@@ -56,29 +68,19 @@ empty ``Pipeline`` object.
5668
Example
5769
~~~~~~~
5870

59-
The following example opens a change stream on the ``db.courses`` collection and
71+
The following example opens a change stream on the ``courses`` collection and
6072
outputs all changes:
6173

62-
.. code-block:: go
63-
64-
coll := client.Database("db").Collection("courses")
65-
66-
// open a change stream with an empty pipeline parameter
67-
changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{})
68-
if err != nil {
69-
panic(err)
70-
}
71-
defer changeStream.Close(context.TODO())
72-
73-
// iterate over the cursor to print the change-stream events
74-
for changeStream.Next(context.TODO()) {
75-
fmt.Println(changeStream.Current)
76-
}
74+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
75+
:language: go
76+
:dedent:
77+
:start-after: begin open stream
78+
:end-before: end open stream
7779

78-
If you modify the ``db.courses`` collection in a separate program or shell, this code will print
80+
If you modify the ``courses`` collection in a separate program or shell, this code will print
7981
your changes as they occur. Inserting a document with a ``title`` value
80-
of "Advanced Screenwriting" and an ``enrollment`` value of ``20``
81-
results in the following change-stream event:
82+
of ``"Advanced Screenwriting"`` and an ``enrollment`` value of ``20``
83+
results in the following change event:
8284

8385
.. code-block:: none
8486
:copyable: false
@@ -108,20 +110,19 @@ You can use the following pipeline stages in this parameter:
108110
Example
109111
~~~~~~~
110112

111-
The following example opens a change stream on the ``db`` database, but only watches for
113+
The following example opens a change stream on the ``db`` database but only watches for
112114
new delete operations:
113115

114-
.. code-block:: go
115-
116-
db := client.Database("db")
117-
118-
pipeline := bson.D{{"$match", bson.D{{"operationType", "delete"}}}}
119-
changeStream, err := db.Watch(context.TODO(), mongo.Pipeline{pipeline})
116+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
117+
:language: go
118+
:dedent:
119+
:start-after: begin delete events
120+
:end-before: end delete events
120121

121122
.. note::
122123

123124
The ``Watch()`` method was called on the ``db`` database, so the code outputs
124-
new delete operations in any collection within this database.
125+
new delete operations on any collection within this database.
125126

126127
Modify the Behavior of ``Watch()``
127128
----------------------------------
@@ -194,28 +195,19 @@ document before a change, set the ``FullDocumentBeforeChange`` field of the
194195
Example
195196
~~~~~~~
196197

197-
The following example calls the ``Watch()`` method on the ``db.courses`` collection. It
198+
The following example calls the ``Watch()`` method on the ``courses`` collection. It
198199
specifies a value for the ``FullDocument`` field of the ``options`` parameter to
199200
output a copy of the entire modified document, instead of only the changed fields:
200201

201-
.. code-block:: go
202-
203-
coll := client.Database("db").Collection("courses")
204-
opts := options.ChangeStream().SetFullDocument(options.UpdateLookup)
205-
206-
changeStream, err := coll.Watch(context.TODO(), mongo.Pipeline{}, opts)
207-
if err != nil {
208-
panic(err)
209-
}
210-
defer changeStream.Close(context.TODO())
211-
212-
for changeStream.Next(context.TODO()) {
213-
fmt.Println(changeStream.Current)
214-
}
202+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/changeStream.go
203+
:language: go
204+
:dedent:
205+
:start-after: begin full document
206+
:end-before: end full document
215207

216208
Updating the ``enrollment`` value of the document with the
217-
``title`` of "World Fiction" from ``35`` to ``30`` results in the
218-
following change-stream event:
209+
``title`` of ``"World Fiction"`` from ``35`` to ``30`` results in the
210+
following change event:
219211

220212
.. code-block:: none
221213
:copyable: false

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ Sample Data
2121
~~~~~~~~~~~
2222

2323
The examples in this section use the following ``Tea`` struct as a model for documents
24-
in the ``ratings`` collection:
24+
in the ``tea`` collection:
2525

2626
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go
2727
:start-after: start-tea-struct
2828
:end-before: end-tea-struct
2929
:language: go
3030
:dedent:
3131

32-
To run the examples in this guide, load the sample data into the ``tea.ratings`` collection with the following
33-
snippet:
32+
To run the examples in this guide, load the sample data into the ``tea`` collection in the ``db``
33+
database with the following snippet:
3434

3535
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/countAndEstimate.go
3636
:language: go
@@ -147,7 +147,8 @@ Example
147147

148148
The following example performs the following actions:
149149

150-
- Counts the number of documents where the ``rating`` is greater than ``5``
150+
- Counts the number of documents in which the value of the ``rating`` field is greater
151+
than ``5``
151152
- Assigns the count to the ``counted_documents`` field
152153

153154
.. io-code-block::
@@ -217,7 +218,7 @@ Example
217218
```````
218219

219220
The following example estimates the number of documents in the
220-
``ratings`` collection:
221+
``tea`` collection:
221222

222223
.. io-code-block::
223224
:copyable: true
@@ -229,13 +230,13 @@ The following example estimates the number of documents in the
229230
if err != nil {
230231
panic(err)
231232
}
232-
fmt.Printf("Estimated number of documents in the ratings collection: %d\n", count)
233+
fmt.Printf("Estimated number of documents in the tea collection: %d\n", count)
233234

234235
.. output::
235236
:language: none
236237
:visible: false
237238

238-
Estimated number of documents in the ratings collection: 9
239+
Estimated number of documents in the tea collection: 9
239240

240241
Additional Information
241242
----------------------

0 commit comments

Comments
 (0)