Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
Returning full documents when you only need two fields is one of the most common accidental performance problems in MongoDB applications. A document with 40 fields sent over the wire 1,000 times per second is dramatically more expensive than projecting to 3 fields — and it's a one-line fix. Pagination matters because skip(n) performs a full scan of the first n documents every time; cursor-based pagination using a range filter on _id or a timestamp scales to billions of documents without degrading, which is why every high-volume API uses it.
Project specific fields, sort results, and implement both offset and cursor-based pagination.
score field. Implement cursor-based pagination using score as the cursor field (instead of _id). Fetch 3 pages of 5 and verify no document appears twice and none are skipped.explain('executionStats') on a query with skip(1000).limit(10). Note the docsExamined value. Then implement cursor pagination for the same result. Compare docsExamined between the two.{ password: 0 } (exclusion projection) on a users collection. Verify the field is absent. Then try mixing inclusion and exclusion in one projection — observe the error MongoDB returns.Use these three in order. Each builds on the one before.
Explain the difference between offset pagination (skip/limit) and cursor-based pagination. Why does offset pagination become slower as the offset grows?
When I project `{ title: 1, _id: 0 }`, at what stage in query execution does MongoDB discard the other fields? Does it read them from storage first?
I need to paginate a feed sorted by a non-unique `score` field. If two documents have the same score, cursor-based pagination on score alone will skip documents. How do I implement stable cursor pagination on a non-unique sort field?
package main
import (
"context"; "fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
client, _ := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
col := client.Database("demo").Collection("posts")
// Project only title+createdAt, sort descending, limit 5
opts := options.Find().
SetProjection(bson.D{{"title", 1}, {"createdAt", 1}}).
SetSort(bson.D{{"createdAt", -1}}).
SetLimit(5)
cursor, _ := col.Find(context.TODO(), bson.D{}, opts)
var results []bson.M
cursor.All(context.TODO(), &results)
for _, r := range results { fmt.Println(r) }
}go run main.go