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.
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.Use these three in order. Each builds on the one before.
Explain MongoDB's document model in one paragraph. What is BSON, how does it differ from JSON, and why does MongoDB use it internally?
Walk me through what happens when I call insertMany() in the MongoDB Node.js driver — from the method call to the data landing on disk. Cover the write concern, the WiredTiger storage engine, and the journal.
I have a collection with 10 million documents. My find() with a field filter is slow. What are the three most likely causes and how do I diagnose each one without guessing?
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