Skip to content

Instantly share code, notes, and snippets.

@prestonvasquez
Last active July 18, 2024 01:56
Show Gist options
  • Save prestonvasquez/4108cf11d01d4858a1cda2d22310c5be to your computer and use it in GitHub Desktop.
Save prestonvasquez/4108cf11d01d4858a1cda2d22310c5be to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
clientOptions := options.Client().
ApplyURI("mongodb://127.0.0.1:8000/?loadBalanced=true").
SetLoadBalanced(true).
SetMaxPoolSize(3)
ctx := context.Background()
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatalf("error connecting to MongoDB: %v", err)
}
defer client.Disconnect(context.Background())
coll := client.Database("db").Collection("test")
coll.InsertOne(ctx, bson.D{{"x", 1}})
coll.InsertOne(ctx, bson.D{{"x", 2}})
coll.InsertOne(ctx, bson.D{{"x", 3}})
for i := 0; i < 4; i++ {
find(ctx, client.Database("db"))
}
}
func find(ctx context.Context, db *mongo.Database) {
_, err := db.RunCommandCursor(ctx, bson.D{
{"find", "test"}, // Collection
{"batchSize", 1},
{"filter", bson.D{{"x", bson.D{{"$gte", 0}}}}},
})
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment