@@ -21,8 +21,17 @@ results from read operations.
2121Sample Data
2222~~~~~~~~~~~
2323
24- To run the example in this guide, load these documents into the
25- ``tea.ratings`` collection with the following
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/skip.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 these documents into the
34+ ``db.courses`` collection with the following
2635snippet:
2736
2837.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go
@@ -31,7 +40,11 @@ snippet:
3140 :start-after: begin insertDocs
3241 :end-before: end insertDocs
3342
34- .. include:: /includes/fundamentals/tea-sample-data-ending.rst
43+ .. include:: /includes/fundamentals/automatic-db-coll-creation.rst
44+
45+ Each document contains a description of a university course that
46+ includes the course title and maximum enrollment, corresponding to
47+ the ``title`` and ``enrollment`` fields.
3548
3649Skip
3750----
@@ -40,8 +53,7 @@ To skip a specified number of returned results from a query, pass the
4053number of documents you want to skip to the ``SetSkip()`` method of
4154the read operation's options.
4255
43- Specify the options as the last parameter to the following read
44- operation methods:
56+ The following read operations take an options object as a parameter:
4557
4658- ``Find()``
4759- ``FindOne()``
@@ -53,20 +65,21 @@ query, that query returns no documents.
5365
5466.. tip::
5567
56- Passing in a negative number to the ``SetSkip()`` method results
57- in a runtime error.
68+ Passing in a negative number to the ``SetSkip()`` method results
69+ in a runtime error.
5870
59- Documents return in a natural order, which can lead to skipping random
60- documents. To avoid this, use a ``SetSort()`` method before the
61- ``SetSkip()`` method.
71+ Find operations return documents in a natural order that is not sorted
72+ on any field. To avoid skipping random documents, use the ``SetSort()``
73+ method to sort documents on a field with unique values before setting a
74+ skip option.
6275
6376Example
6477~~~~~~~
6578
66- The following example performs the following actions in order with the
67- ``Find()`` method :
79+ The following example performs a ``Find()`` operation with the following
80+ behavior :
6881
69- - An ascedning sort on the ``rating `` field
82+ - Sorts results in ascending order on the ``enrollment `` field
7083- Skips the first two documents
7184
7285.. io-code-block::
@@ -75,72 +88,73 @@ The following example performs the following actions in order with the
7588 .. input::
7689 :language: go
7790
78- filter := bson.D{}
79- opts := options.Find().SetSort(bson.D{{"rating", 1}}).SetSkip(2)
80-
81- cursor, err := coll.Find(context.TODO(), filter, opts)
82-
83- var results []bson.D
91+ opts := options.Find().SetSort(bson.D{{"enrollment", 1}}).SetSkip(2)
92+
93+ cursor, err := coll.Find(context.TODO(), bson.D{}, opts)
94+
95+ var results []Course
8496 if err = cursor.All(context.TODO(), &results); err != nil {
85- panic(err)
97+ panic(err)
8698 }
8799 for _, result := range results {
88- fmt.Println(result)
100+ res, _ := json.Marshal(result)
101+ fmt.Println(string(res))
89102 }
90103
91104 .. output::
92105 :language: none
93106 :visible: false
94107
95- [{_id ObjectID("...")} {type Oolong} {rating 7}]
96- [{_id ObjectID("...")} {type Earl Grey} {rating 8}]
97- [{_id ObjectID("...")} {type Masala} {rating 10}]
108+ {"Title":"Plate Tectonics","Enrollment":45}
109+ {"Title":"Abstract Algebra","Enrollment":60}
98110
99111.. _golang-skip-aggregation:
100112
101113Aggregation
102114-----------
103115
104116You can also include the :manual:`$skip </reference/operator/aggregation/skip/>`
105- stage to specify a skip in an aggregation pipeline.
117+ stage in an aggregation pipeline to skip documents .
106118
107119Example
108120~~~~~~~
109121
110- The following example performs the following actions in order with the
111- ``Aggregate()`` method :
122+ The following example performs an ``Aggregate()`` operation with the following
123+ behavior :
112124
113- - A descending sort on the ``rating `` field
114- - Skips the first three documents
125+ - Sorts results in descending order on the ``enrollment `` field
126+ - Skips the first document
115127
116128.. io-code-block::
117129 :copyable: true
118130
119131 .. input::
120132 :language: go
121133
122- sortStage := bson.D{{"$sort", bson.D{{"rating ", -1}}}}
123- skipStage := bson.D{{"$skip", 3 }}
124-
134+ sortStage := bson.D{{"$sort", bson.D{{"enrollment ", -1}}}}
135+ skipStage := bson.D{{"$skip", 1 }}
136+
125137 cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{sortStage, skipStage})
126138 if err != nil {
127- panic(err)
139+ panic(err)
128140 }
129-
130- var results []bson.D
141+
142+ var results []Course
131143 if err = cursor.All(context.TODO(), &results); err != nil {
132- panic(err)
144+ panic(err)
133145 }
134146 for _, result := range results {
135- fmt.Println(result)
147+ res, _ := json.Marshal(result)
148+ fmt.Println(string(res))
136149 }
137150
138151 .. output::
139152 :language: none
140153 :visible: false
141154
142- [{_id ObjectID("...")} {type Assam} {rating 5}]
143- [{_id ObjectID("...")} {type English Breakfast} {rating 5}]
155+ {"Title":"Plate Tectonics","Enrollment":45}
156+ {"Title":"World Fiction","Enrollment":35}
157+ {"Title":"Modern Poetry","Enrollment":12}
144158
145159Additional Information
146160----------------------
0 commit comments