@@ -21,17 +21,29 @@ returned from a read operation.
21
21
Sample Data
22
22
~~~~~~~~~~~
23
23
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:
27
35
28
36
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/limit.go
29
37
:language: go
30
38
:dedent:
31
39
:start-after: begin insertDocs
32
40
:end-before: end insertDocs
33
41
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.
35
47
36
48
Limit
37
49
-----
@@ -40,66 +52,65 @@ To limit the number of documents returned from a query, pass the
40
52
number of documents you want returned to the ``SetLimit()`` method of
41
53
the read operation's options.
42
54
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:
45
56
46
57
- ``Find()``
47
58
- ``CountDocuments()``
48
59
- ``gridfs.Bucket.Find()``
49
60
50
61
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
54
65
documents.
55
66
56
67
Example
57
68
~~~~~~~
58
69
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:
60
72
61
73
.. io-code-block::
62
74
:copyable: true
63
75
64
76
.. input::
65
77
:language: go
66
78
67
- filter := bson.D{}
79
+ filter := bson.D{{"enrollment", bson.D{{"$gt", 20}}} }
68
80
opts := options.Find().SetLimit(2)
69
-
81
+
70
82
cursor, err := coll.Find(context.TODO(), filter, opts)
71
-
72
- var results []bson.D
83
+
84
+ var results []Course
73
85
if err = cursor.All(context.TODO(), &results); err != nil {
74
- panic(err)
86
+ panic(err)
75
87
}
76
88
for _, result := range results {
77
- fmt.Println(result)
89
+ res, _ := json.Marshal(result)
90
+ fmt.Println(string(res))
78
91
}
79
92
80
93
.. output::
81
94
:language: none
82
95
:visible: false
83
96
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}
86
99
87
100
Multiple Options
88
101
----------------
89
102
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.
93
105
94
106
Example
95
107
~~~~~~~
96
108
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:
99
110
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
103
114
104
115
.. io-code-block::
105
116
:copyable: true
@@ -108,37 +119,38 @@ The following example performs the following actions in order using the
108
119
:language: go
109
120
110
121
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
+
113
124
cursor, err := coll.Find(context.TODO(), filter, opts)
114
-
115
- var results []bson.D
125
+
126
+ var results []Course
116
127
if err = cursor.All(context.TODO(), &results); err != nil {
117
- panic(err)
128
+ panic(err)
118
129
}
119
130
for _, result := range results {
120
- fmt.Println(result)
131
+ res, _ := json.Marshal(result)
132
+ fmt.Println(string(res))
121
133
}
122
134
123
135
.. output::
124
136
:language: none
125
137
:visible: false
126
138
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}
129
141
130
142
.. tip::
131
143
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:
133
145
134
146
.. code-block:: go
135
147
:copyable: false
136
148
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}})
142
154
143
155
.. _golang-limit-aggregation:
144
156
@@ -160,27 +172,28 @@ The following example shows how to return three documents:
160
172
:language: go
161
173
162
174
limitStage := bson.D{{"$limit", 3}}
163
-
175
+
164
176
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
165
177
if err != nil {
166
- panic(err)
178
+ panic(err)
167
179
}
168
-
169
- var results []bson.D
180
+
181
+ var results []Course
170
182
if err = cursor.All(context.TODO(), &results); err != nil {
171
- panic(err)
183
+ panic(err)
172
184
}
173
185
for _, result := range results {
174
- fmt.Println(result)
186
+ res, _ := json.Marshal(result)
187
+ fmt.Println(string(res))
175
188
}
176
189
177
190
.. output::
178
191
:language: none
179
192
:visible: false
180
193
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}
184
197
185
198
Additional Information
186
199
----------------------
0 commit comments