Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
Every MongoDB interaction starts with a client connection and a collection handle. Understanding connection strings, authentication, and the insert/find cycle is the bedrock everything else builds on. A misconfigured connection string causes silent failures that look like data loss; knowing exactly what the driver does on connect and on query prevents hours of debugging. The document model — JSON stored as BSON — means your first insert looks almost identical in every language, which makes MongoDB one of the fastest databases to get productive with.
Connect to a local MongoDB instance, insert two user documents, then query them back by a field value.
age values. Query for those with age between 25 and 35 using $gte and $lte in a single filter. Verify the count matches what you inserted.age field. Query { age: { $exists: false } } and confirm it appears. Then query { age: { $gte: 0 } } and confirm it does NOT appear.insertOne and capture the returned insertedId. Use findOne({ _id: insertedId }) to retrieve the exact document you just inserted.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"))
defer client.Disconnect(context.TODO())
col := client.Database("demo").Collection("users")
col.InsertMany(context.TODO(), []interface{}{
bson.D{{"name", "Alice"}, {"age", 30}},
bson.D{{"name", "Bob"}, {"age", 25}},
})
cursor, _ := col.Find(context.TODO(), bson.D{{"age", bson.D{{"$gte", 28}}}})
var results []bson.M
cursor.All(context.TODO(), &results)
for _, r := range results {
fmt.Println(r)
}
}go run main.go