Skip to content

Commit e9ac81f

Browse files
DOCSP-19037 updated aggregation examples (#68)
* updated aggregation examples
1 parent 2ec9f85 commit e9ac81f

File tree

7 files changed

+92
-55
lines changed

7 files changed

+92
-55
lines changed

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Limit the Number of Returned Results
77
.. contents:: On this page
88
:local:
99
:backlinks: none
10-
:depth: 2
10+
:depth: 1
1111
:class: singlecol
1212

1313
Overview
@@ -61,29 +61,14 @@ The following example shows how to return two documents:
6161
:start-after: begin limit
6262
:end-before: end limit
6363

64-
After running the preceding example, the output resembles the following:
64+
After running this example, the output resembles the following:
6565

6666
.. code-block:: none
6767
:copyable: false
6868

69-
//results truncated
7069
[{_id ObjectID("...")} {type Masala} {rating 10}]
7170
[{_id ObjectID("...")} {type Assam} {rating 5}]
7271

73-
.. tip:: Using Aggregation
74-
75-
You can also include the :manual:`$limit </reference/operator/aggregation/limit/>`
76-
stage to specify a limit in an aggregation pipeline.
77-
78-
The following example specifies the same limit from the
79-
preceding example in an aggregation pipeline:
80-
81-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/limit.go
82-
:language: go
83-
:dedent:
84-
:start-after: begin aggregate limit
85-
:end-before: end aggregate limit
86-
8772
Multiple Options
8873
----------------
8974

@@ -107,12 +92,11 @@ The following example performs the following actions in order using the
10792
:start-after: begin multi options
10893
:end-before: end multi options
10994

110-
After running the preceding example, the output resembles the following:
95+
After running this example, the output resembles the following:
11196

11297
.. code-block:: none
11398
:copyable: false
11499

115-
//results truncated
116100
[{_id ObjectID("...")} {type Earl Grey} {rating 8}]
117101
[{_id ObjectID("...")} {type Oolong} {rating 7}]
118102

@@ -129,6 +113,32 @@ After running the preceding example, the output resembles the following:
129113
multiOptions := options.Find().SetSkip(1).SetSort(bson.D{{"rating", -1}}).SetLimit(2)
130114
multiOptions := options.Find().SetSkip(1).SetLimit(2).SetSort(bson.D{{"rating", -1}})
131115

116+
Aggregation
117+
-----------
118+
119+
You can also include the :manual:`$limit </reference/operator/aggregation/limit/>`
120+
stage to specify a limit in an aggregation pipeline.
121+
122+
Example
123+
~~~~~~~
124+
125+
The following example shows how to return three documents:
126+
127+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/limit.go
128+
:language: go
129+
:dedent:
130+
:start-after: begin aggregate limit
131+
:end-before: end aggregate limit
132+
133+
After running this example, the output resembles the following:
134+
135+
.. code-block:: none
136+
:copyable: false
137+
138+
[{_id ObjectID("...")} {type Masala} {rating 10}]
139+
[{_id ObjectID("...")} {type Assam} {rating 5}]
140+
[{_id ObjectID("...")} {type Oolong} {rating 7}]
141+
132142
Additional Information
133143
----------------------
134144

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ between ``5`` and ``10``:
7676
:start-after: begin find docs
7777
:end-before: end find docs
7878

79-
After running the preceding example, the output resembles the following:
79+
After running this example, the output resembles the following:
8080

8181
.. code-block:: none
8282
:copyable: false
8383

84-
//results truncated
8584
[_id:ObjectID("...") type:Masala rating:7 ]
8685
[_id:ObjectID("...") type:Earl Grey rating:9 ]
8786

@@ -123,7 +122,7 @@ The following example passes a context and an aggregation pipeline that:
123122
:start-after: begin aggregate docs
124123
:end-before: end aggregate docs
125124

126-
After running the preceding example, the output resembles the following:
125+
After running this example, the output resembles the following:
127126

128127
.. code-block:: none
129128
:copyable: false

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Skip Returned Results
77
.. contents:: On this page
88
:local:
99
:backlinks: none
10-
:depth: 2
10+
:depth: 1
1111
:class: singlecol
1212

1313
Overview
@@ -63,7 +63,7 @@ Example
6363
The following example performs the following actions in order with the
6464
``Find()`` function:
6565

66-
- Sorts all the matched documents in ascending order
66+
- An ascedning sort on the ``rating`` field
6767
- Skips the first two documents
6868

6969
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go
@@ -72,30 +72,44 @@ The following example performs the following actions in order with the
7272
:start-after: begin skip
7373
:end-before: end skip
7474

75-
After running the preceding example, the output resembles the following:
75+
After running this example, the output resembles the following:
7676

7777
.. code-block:: none
7878
:copyable: false
79-
80-
//results truncated
79+
8180
[{_id ObjectID("...")} {type Oolong} {rating 7}]
8281
[{_id ObjectID("...")} {type Earl Grey} {rating 8}]
8382
[{_id ObjectID("...")} {type Masala} {rating 10}]
8483

85-
.. tip:: Using Aggregation
84+
Aggregation
85+
-----------
8686

87-
You can also include the :manual:`$skip </reference/operator/aggregation/skip/>`
88-
stage to specify a skip in an aggregation pipeline.
87+
You can also include the :manual:`$skip </reference/operator/aggregation/skip/>`
88+
stage to specify a skip in an aggregation pipeline.
8989

90-
The following example specifies the same sort and skip from the
91-
preceding example in an aggregation pipeline:
90+
Example
91+
~~~~~~~
9292

93-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go
94-
:language: go
95-
:dedent:
96-
:start-after: begin aggregate skip
97-
:end-before: end aggregate skip
93+
The following example performs the following actions in order with the
94+
``Aggregate()`` function:
95+
96+
- A descending sort on the ``rating`` field
97+
- Skips the first three documents
98+
99+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go
100+
:language: go
101+
:dedent:
102+
:start-after: begin aggregate skip
103+
:end-before: end aggregate skip
104+
105+
After running this example, the output resembles the following:
106+
107+
.. code-block:: none
108+
:copyable: false
98109

110+
[{_id ObjectID("...")} {type Assam} {rating 5}]
111+
[{_id ObjectID("...")} {type English Breakfast} {rating 5}]
112+
99113
Additional Information
100114
----------------------
101115

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,11 @@ The following example specifies an ascending sort on the ``rating`` field:
7676
:end-before: end ascending sort
7777
:emphasize-lines: 2-3, 5
7878

79-
After running the preceding example, the output resembles the following:
79+
After running this example, the output resembles the following:
8080

8181
.. code-block:: none
8282
:copyable: false
8383

84-
//results truncated
8584
[{_id ObjectID("...")} {type Assam} {rating 5}]
8685
[{_id ObjectID("...")} {type English Breakfast} {rating 5}]
8786
[{_id ObjectID("...")} {type Oolong} {rating 7}]
@@ -114,7 +113,7 @@ The following example specifies a descending sort on the ``rating`` field:
114113
:end-before: end descending sort
115114
:emphasize-lines: 2-3, 5
116115

117-
After running the preceding example, the output resembles the following:
116+
After running this example, the output resembles the following:
118117

119118
.. code-block:: none
120119
:copyable: false
@@ -157,7 +156,7 @@ then a descending sort on the ``type`` field:
157156
:end-before: end multi sort
158157
:emphasize-lines: 2-3, 5
159158

160-
After running the preceding example, the output resembles the following:
159+
After running this example, the output resembles the following:
161160

162161
.. code-block:: none
163162
:copyable: false
@@ -168,19 +167,34 @@ After running the preceding example, the output resembles the following:
168167
[{_id ObjectID("...")} {type Earl Grey} {rating 8}]
169168
[{_id ObjectID("...")} {type Masala} {rating 10}]
170169

171-
.. tip:: Using Aggregation
170+
Aggregation
171+
~~~~~~~~~~~
172+
173+
You can also include the :manual:`$sort </reference/operator/aggregation/sort/>`
174+
stage to specify a sort in an aggregation pipeline.
175+
176+
Example
177+
```````
172178

173-
You can also include the :manual:`$sort </reference/operator/aggregation/sort/>`
174-
stage to specify a sort in an aggregation pipeline.
179+
The following example specifies a descending sort on the ``rating``
180+
field, then an ascending sort on the ``type`` field:
181+
182+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort.go
183+
:language: go
184+
:dedent:
185+
:start-after: begin aggregate sort
186+
:end-before: end aggregate sort
175187

176-
The following example specifies the same sort from the preceding
177-
example in an aggregation pipeline:
188+
After running this example, the output resembles the following:
178189

179-
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort.go
180-
:language: go
181-
:dedent:
182-
:start-after: begin aggregate sort
183-
:end-before: end aggregate sort
190+
.. code-block:: none
191+
:copyable: false
192+
193+
[{_id ObjectID("...")} {type Masala} {rating 10}]
194+
[{_id ObjectID("...")} {type Earl Grey} {rating 8}]
195+
[{_id ObjectID("...")} {type Oolong} {rating 7}]
196+
[{_id ObjectID("...")} {type Assam} {rating 5}]
197+
[{_id ObjectID("...")} {type English Breakfast} {rating 5}]
184198

185199
Additional Information
186200
----------------------

source/includes/fundamentals/code-snippets/CRUD/limit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func main() {
8181

8282
fmt.Println("Aggregation Limit:")
8383
// begin aggregate limit
84-
limitStage := bson.D{{"$limit", 2}}
84+
limitStage := bson.D{{"$limit", 3}}
8585

8686
aggCursor, aggErr := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
8787
if aggErr != nil {

source/includes/fundamentals/code-snippets/CRUD/skip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func main() {
6565

6666
fmt.Println("Aggegation Skip:")
6767
// begin aggregate skip
68-
sortStage := bson.D{{"$sort", bson.D{{"rating", 1}}}}
69-
skipStage := bson.D{{"$skip", 2}}
68+
sortStage := bson.D{{"$sort", bson.D{{"rating", -1}}}}
69+
skipStage := bson.D{{"$skip", 3}}
7070

7171
aggCursor, aggErr := coll.Aggregate(context.TODO(), mongo.Pipeline{sortStage, skipStage})
7272
if aggErr != nil {

source/includes/fundamentals/code-snippets/CRUD/sort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func main() {
100100

101101
fmt.Println("Aggregation Sort:")
102102
// begin aggregate sort
103-
sortStage := bson.D{{"$sort", bson.D{{"rating", 1}, {"type", -1}}}}
103+
sortStage := bson.D{{"$sort", bson.D{{"rating", -1}, {"type", 1}}}}
104104

105105
aggCursor, aggErr := coll.Aggregate(context.TODO(), mongo.Pipeline{sortStage})
106106
if aggErr != nil {

0 commit comments

Comments
 (0)