Skip to content

Instantly share code, notes, and snippets.

@nikolaymatrosov
Created April 5, 2021 19:56
Show Gist options
  • Save nikolaymatrosov/b54ba350d3f6497af793c3de0bc3da2a to your computer and use it in GitHub Desktop.
Save nikolaymatrosov/b54ba350d3f6497af793c3de0bc3da2a to your computer and use it in GitHub Desktop.
MDB demo
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"time"
)
func main() {
uri := fmt.Sprintf("mongodb://%s:%s@%s/%s?replicaSet=%s&ssl=true&sslCertificateAuthorityFile=%s",
"user1",
"<password>",
"rc1b-<your-host>.mdb.yandexcloud.net:27018",
"db1",
"rs01",
"/usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt",
)
client, err := mongo.NewClient(options.Client().ApplyURI(uri), )
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
panic(err)
}
// Test connection
db := client.Database("db1")
coll := db.Collection("inventory_insert")
result, err := coll.InsertOne(
context.Background(),
bson.D{
{"item", "canvas"},
{"qty", 100},
{"tags", bson.A{"cotton"}},
{"size", bson.D{
{"h", 28},
{"w", 35.5},
{"uom", "cm"},
}},
})
fmt.Printf("%+v", result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment