Docs Menu

Skip Returned Results

On this page

  • Overview
  • Skip
  • Aggregation
  • Additional Information

In this guide, you can learn how to skip a specified number of returned results from read operations.

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

The server implicitly creates the necessary database and collection when you perform a write operation against them if they don't already exist.

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.

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.

The following example performs the following actions in order with the Find() method:

  • An ascedning sort on the rating field
  • Skips 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)
}

You can also include the $skip stage to specify a skip in an aggregation pipeline.

The following example performs the following actions in order with the Aggregate() method:

  • A descending sort on the rating field
  • Skips 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)
}

To learn more about the operations mentioned, see the following guides:

To learn more about any of the methods or types discussed in this guide, see the following API Documentation:

←  Sort ResultsLimit the Number of Returned Results →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.