Skip Returned Results
On this page
Overview
In this guide, you can learn how to skip a specified number of returned results from read operations.
Sample Data
To run the example in this guide, load these documents into the
tea.ratings
collection with the following
snippet:
coll := client.Database("tea").Collection("ratings") docs := []interface{}{ bson.D{{"type", "Masala"}, {"rating", 10}}, bson.D{{"type", "Assam"}, {"rating", 5}}, bson.D{{"type", "Oolong"}, {"rating", 7}}, bson.D{{"type", "Earl Grey"}, {"rating", 8}}, bson.D{{"type", "English Breakfast"}, {"rating", 5}}, } result, err := coll.InsertMany(context.TODO(), docs) if err != nil { panic(err) } fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
Tip
Non-existent Databases and Collections
If the necessary database and collection don't exist when you perform a write operation, the server implicitly creates them.
Each document contains a rating for a type of tea that corresponds to
the type
and rating
fields.
Note
Each example truncates the ObjectID
value because the driver
generates them uniquely.
Skip
To skip a specified number of returned results from a query, pass the
number of documents you want to skip to the SetSkip()
method of
the read operation's options.
Specify the options as the last parameter to the following read operation methods:
Find()
FindOne()
CountDocuments()
gridfs.Bucket.Find()
If the number of documents exceeds the number of matched documents for a query, that query returns no documents.
Tip
Passing in a negative number to the SetSkip()
method results
in a runtime error.
Documents return in a natural order, which can lead to skipping random
documents. To avoid this, use a SetSort()
method before the
SetSkip()
method.
Example
The following example performs the following actions in order with the
Find()
method:
An ascedning sort on the
rating
fieldSkips the first two documents
filter := bson.D{} opts := options.Find().SetSort(bson.D{{"rating", 1}}).SetSkip(2) cursor, err := coll.Find(context.TODO(), filter, opts) var results []bson.D if err = cursor.All(context.TODO(), &results); err != nil { panic(err) } for _, result := range results { fmt.Println(result) }
Aggregation
You can also include the $skip stage to specify a skip in an aggregation pipeline.
Example
The following example performs the following actions in order with the
Aggregate()
method:
A descending sort on the
rating
fieldSkips the first three documents
sortStage := bson.D{{"$sort", bson.D{{"rating", -1}}}} skipStage := bson.D{{"$skip", 3}} cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{sortStage, skipStage}) if err != nil { panic(err) } var results []bson.D if err = cursor.All(context.TODO(), &results); err != nil { panic(err) } for _, result := range results { fmt.Println(result) }
Additional Information
To learn more about the operations mentioned, see the following guides:
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API Documentation: