Created
April 5, 2021 19:56
-
-
Save nikolaymatrosov/b54ba350d3f6497af793c3de0bc3da2a to your computer and use it in GitHub Desktop.
MDB demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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