Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
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.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