@@ -21,17 +21,29 @@ returned from a read operation.
2121Sample 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
3648Limit
3749-----
@@ -40,66 +52,65 @@ To limit the number of documents returned from a query, pass the
4052number of documents you want returned to the ``SetLimit()`` method of
4153the 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
5061If 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
5465documents.
5566
5667Example
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
87100Multiple 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
94106Example
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
185198Additional Information
186199----------------------
0 commit comments