Use Coupon Code GOPHERCON25 for $75 Atlas Credit
cloud.mongodb.com
import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
)
client, err := mongo.NewClient(
options.Client().ApplyURI("mongodb://localhost:27017")
)
if err != nil { log.Fatal(err) }
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil { log.Fatal(err) }
collection := client.Database("testing").Collection("numbers")
res, err := collection.InsertOne(ctx, bson.M{"value": "Hi"})
if err != nil { log.Fatal(err) }
id := res.InsertedID
fmt.Println("Inserted document: ", id)
cur, err := collection.Find(ctx, bson.D{})
if err != nil { log.Fatal(err) }
defer cur.Close(ctx)
for cur.Next(ctx) {
var result bson.M
err := cur.Decode(&result)
if err != nil { log.Fatal(err) }
fmt.Println(result)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
Documentation at godoc.org/go.mongodb.org/mongo-driver
Migration guide for mgo users: mongodb.com/blog/post/go-migration-guide
Get started in under an hour, for free!
Use coupon code GOPHERCON25 for $75 Atlas Credit
cloud.mongodb.com