Skip to content

Commit 4c8b8f8

Browse files
authored
DOCSP-26441: use struct in limit pg (#218)
* DOCSP-26441: use struct in limit pg * DB PR fixes 1
1 parent 8c7a538 commit 4c8b8f8

File tree

3 files changed

+94
-71
lines changed

3 files changed

+94
-71
lines changed

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

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,29 @@ returned from a read operation.
2121
Sample Data
2222
~~~~~~~~~~~
2323

24-
To run the examples in this guide, load these documents into the
25-
``tea.ratings`` collection with the following
26-
snippet:
24+
The examples in this guide use the following ``Course`` struct as a model for documents
25+
in the ``courses`` collection:
26+
27+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/limit.go
28+
:start-after: start-course-struct
29+
:end-before: end-course-struct
30+
:language: go
31+
:dedent:
32+
33+
To run the examples in this guide, load the sample data into the
34+
``db.courses`` collection with the following snippet:
2735

2836
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/limit.go
2937
:language: go
3038
:dedent:
3139
:start-after: begin insertDocs
3240
:end-before: end insertDocs
3341

34-
.. include:: /includes/fundamentals/tea-sample-data-ending.rst
42+
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
43+
44+
Each document contains a description of a university course that
45+
includes the course title and maximum enrollment, corresponding to
46+
the ``title`` and ``enrollment`` fields in each document.
3547

3648
Limit
3749
-----
@@ -40,66 +52,65 @@ To limit the number of documents returned from a query, pass the
4052
number of documents you want returned to the ``SetLimit()`` method of
4153
the read operation's options.
4254

43-
Specify the options as the last parameter to the following read
44-
operation methods:
55+
The following read operations take an options object as a parameter:
4556

4657
- ``Find()``
4758
- ``CountDocuments()``
4859
- ``gridfs.Bucket.Find()``
4960

5061
If the limit is ``0`` or exceeds the number of matched
51-
documents, the method returns all the documents. If the limit is a
52-
negative number, the method behaves as if the limit was the absolute
53-
value of the negative number and closes the cursor after retrieving
62+
documents, the method returns all the documents. If the limit is a
63+
negative number, the method uses the absolute value of the negative
64+
number as the limit and closes the cursor after retrieving
5465
documents.
5566

5667
Example
5768
~~~~~~~
5869

59-
The following example shows how to return two documents:
70+
The following example shows how to return two documents that have an
71+
``enrollment`` field value greater than 20:
6072

6173
.. io-code-block::
6274
:copyable: true
6375

6476
.. input::
6577
:language: go
6678

67-
filter := bson.D{}
79+
filter := bson.D{{"enrollment", bson.D{{"$gt", 20}}}}
6880
opts := options.Find().SetLimit(2)
69-
81+
7082
cursor, err := coll.Find(context.TODO(), filter, opts)
71-
72-
var results []bson.D
83+
84+
var results []Course
7385
if err = cursor.All(context.TODO(), &results); err != nil {
74-
panic(err)
86+
panic(err)
7587
}
7688
for _, result := range results {
77-
fmt.Println(result)
89+
res, _ := json.Marshal(result)
90+
fmt.Println(string(res))
7891
}
7992

8093
.. output::
8194
:language: none
8295
:visible: false
8396

84-
[{_id ObjectID("...")} {type Masala} {rating 10}]
85-
[{_id ObjectID("...")} {type Assam} {rating 5}]
97+
{"Title":"Concepts in Topology","Enrollment":35}
98+
{"Title":"Ancient Greece","Enrollment":100}
8699

87100
Multiple Options
88101
----------------
89102

90-
If you configure other options alongside the ``SetLimit()`` method,
91-
the driver performs the limit last regardless of the order you list
92-
the options.
103+
The driver performs the limit behavior last regardless of the order in which you set
104+
any other options.
93105

94106
Example
95107
~~~~~~~
96108

97-
The following example performs the following actions in order using the
98-
``Find()`` method:
109+
The following example performs a ``Find()`` operation with the following behavior:
99110

100-
- Sort the ``rating`` field in descending order
101-
- Skip the first document
102-
- Return the first two of the remaining documents
111+
- Sorts the results in descending order on the ``enrollment`` field
112+
- Skips the first document
113+
- Returns the first two of the remaining documents
103114

104115
.. io-code-block::
105116
:copyable: true
@@ -108,37 +119,38 @@ The following example performs the following actions in order using the
108119
:language: go
109120

110121
filter := bson.D{}
111-
opts := options.Find().SetSort(bson.D{{"rating", -1}}).SetLimit(2).SetSkip(1)
112-
122+
opts := options.Find().SetSort(bson.D{{"enrollment", -1}}).SetLimit(2).SetSkip(1)
123+
113124
cursor, err := coll.Find(context.TODO(), filter, opts)
114-
115-
var results []bson.D
125+
126+
var results []Course
116127
if err = cursor.All(context.TODO(), &results); err != nil {
117-
panic(err)
128+
panic(err)
118129
}
119130
for _, result := range results {
120-
fmt.Println(result)
131+
res, _ := json.Marshal(result)
132+
fmt.Println(string(res))
121133
}
122134

123135
.. output::
124136
:language: none
125137
:visible: false
126138

127-
[{_id ObjectID("...")} {type Earl Grey} {rating 8}]
128-
[{_id ObjectID("...")} {type Oolong} {rating 7}]
139+
{"Title":"Physiology I","Enrollment":60}
140+
{"Title":"Concepts in Topology","Enrollment":35}
129141

130142
.. tip::
131143

132-
Using any of the following option declarations also produce the same result:
144+
Using any of the following option configurations also produces the same result:
133145

134146
.. code-block:: go
135147
:copyable: false
136148

137-
multiOptions := options.Find().SetSort(bson.D{{"rating", -1}}).SetSkip(1).SetLimit(2)
138-
multiOptions := options.Find().SetLimit(2).SetSort(bson.D{{"rating", -1}}).SetSkip(1)
139-
multiOptions := options.Find().SetLimit(2).SetSkip(1).SetSort(bson.D{{"rating", -1}})
140-
multiOptions := options.Find().SetSkip(1).SetSort(bson.D{{"rating", -1}}).SetLimit(2)
141-
multiOptions := options.Find().SetSkip(1).SetLimit(2).SetSort(bson.D{{"rating", -1}})
149+
opts := options.Find().SetSort(bson.D{{"enrollment", -1}}).SetSkip(1).SetLimit(2)
150+
opts := options.Find().SetLimit(2).SetSort(bson.D{{"enrollment", -1}}).SetSkip(1)
151+
opts := options.Find().SetLimit(2).SetSkip(1).SetSort(bson.D{{"enrollment", -1}})
152+
opts := options.Find().SetSkip(1).SetSort(bson.D{{"enrollment", -1}}).SetLimit(2)
153+
opts := options.Find().SetSkip(1).SetLimit(2).SetSort(bson.D{{"enrollment", -1}})
142154

143155
.. _golang-limit-aggregation:
144156

@@ -160,27 +172,28 @@ The following example shows how to return three documents:
160172
:language: go
161173

162174
limitStage := bson.D{{"$limit", 3}}
163-
175+
164176
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
165177
if err != nil {
166-
panic(err)
178+
panic(err)
167179
}
168-
169-
var results []bson.D
180+
181+
var results []Course
170182
if err = cursor.All(context.TODO(), &results); err != nil {
171-
panic(err)
183+
panic(err)
172184
}
173185
for _, result := range results {
174-
fmt.Println(result)
186+
res, _ := json.Marshal(result)
187+
fmt.Println(string(res))
175188
}
176189

177190
.. output::
178191
:language: none
179192
:visible: false
180193

181-
[{_id ObjectID("...")} {type Masala} {rating 10}]
182-
[{_id ObjectID("...")} {type Assam} {rating 5}]
183-
[{_id ObjectID("...")} {type Oolong} {rating 7}]
194+
{"Title":"Romantic Era Music","Enrollment":15}
195+
{"Title":"Concepts in Topology","Enrollment":35}
196+
{"Title":"Ancient Greece","Enrollment":100}
184197

185198
Additional Information
186199
----------------------

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ from read operations.
2121
Sample Data
2222
~~~~~~~~~~~
2323

24-
The example in this guide uses the following ``Course`` struct as a model for documents
24+
The examples in this guide use the following ``Course`` struct as a model for documents
2525
in the ``courses`` collection:
2626

2727
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/sort.go

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"log"
78
"os"
@@ -11,6 +12,14 @@ import (
1112
"go.mongodb.org/mongo-driver/mongo/options"
1213
)
1314

15+
// start-course-struct
16+
type Course struct {
17+
Title string
18+
Enrollment int32
19+
}
20+
21+
// end-course-struct
22+
1423
func main() {
1524
var uri string
1625
if uri = os.Getenv("MONGODB_URI"); uri == "" {
@@ -28,62 +37,62 @@ func main() {
2837
}
2938
}()
3039

31-
client.Database("tea").Collection("ratings").Drop(context.TODO())
32-
3340
// begin insertDocs
34-
coll := client.Database("tea").Collection("ratings")
41+
coll := client.Database("db").Collection("courses")
3542
docs := []interface{}{
36-
bson.D{{"type", "Masala"}, {"rating", 10}},
37-
bson.D{{"type", "Assam"}, {"rating", 5}},
38-
bson.D{{"type", "Oolong"}, {"rating", 7}},
39-
bson.D{{"type", "Earl Grey"}, {"rating", 8}},
40-
bson.D{{"type", "English Breakfast"}, {"rating", 5}},
43+
Course{Title: "Romantic Era Music", Enrollment: 15},
44+
Course{Title: "Concepts in Topology", Enrollment: 35},
45+
Course{Title: "Ancient Greece", Enrollment: 100},
46+
Course{Title: "Physiology I", Enrollment: 60},
4147
}
4248

4349
result, err := coll.InsertMany(context.TODO(), docs)
50+
//end insertDocs
51+
4452
if err != nil {
4553
panic(err)
4654
}
4755
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
48-
//end insertDocs
4956

50-
fmt.Println("Limit:")
57+
fmt.Println("\nLimit:\n")
5158
{
5259
//begin limit
53-
filter := bson.D{}
60+
filter := bson.D{{"enrollment", bson.D{{"$gt", 20}}}}
5461
opts := options.Find().SetLimit(2)
5562

5663
cursor, err := coll.Find(context.TODO(), filter, opts)
5764

58-
var results []bson.D
65+
var results []Course
5966
if err = cursor.All(context.TODO(), &results); err != nil {
6067
panic(err)
6168
}
6269
for _, result := range results {
63-
fmt.Println(result)
70+
res, _ := json.Marshal(result)
71+
fmt.Println(string(res))
6472
}
6573
//end limit
6674
}
6775

68-
fmt.Println("Limit, Skip and Sort:")
76+
fmt.Println("\nLimit, Skip, and Sort:\n")
6977
{
7078
//begin multi options
7179
filter := bson.D{}
72-
opts := options.Find().SetSort(bson.D{{"rating", -1}}).SetLimit(2).SetSkip(1)
80+
opts := options.Find().SetSort(bson.D{{"enrollment", -1}}).SetLimit(2).SetSkip(1)
7381

7482
cursor, err := coll.Find(context.TODO(), filter, opts)
7583

76-
var results []bson.D
84+
var results []Course
7785
if err = cursor.All(context.TODO(), &results); err != nil {
7886
panic(err)
7987
}
8088
for _, result := range results {
81-
fmt.Println(result)
89+
res, _ := json.Marshal(result)
90+
fmt.Println(string(res))
8291
}
8392
//end multi options
8493
}
8594

86-
fmt.Println("Aggregation Limit:")
95+
fmt.Println("\nAggregation Limit:\n")
8796
{
8897
// begin aggregate limit
8998
limitStage := bson.D{{"$limit", 3}}
@@ -93,13 +102,14 @@ func main() {
93102
panic(err)
94103
}
95104

96-
var results []bson.D
105+
var results []Course
97106
if err = cursor.All(context.TODO(), &results); err != nil {
98107
panic(err)
99108
}
100109
for _, result := range results {
101-
fmt.Println(result)
110+
res, _ := json.Marshal(result)
111+
fmt.Println(string(res))
102112
}
103113
// end aggregate limit
104114
}
105-
}
115+
}

0 commit comments

Comments
 (0)