Skip to content

Instantly share code, notes, and snippets.

@amsokol
Created December 16, 2018 11:46
Show Gist options
  • Save amsokol/d9efdfb6128fe65a2a86eadc494529ff to your computer and use it in GitHub Desktop.
Save amsokol/d9efdfb6128fe65a2a86eadc494529ff to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"time"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/wrappers"
)
func main() {
log.Printf("connecting to MongoDB...")
// Create MongoDB client
// NOTE: "mongodb+srv" protocol means connect to Altas cloud MongoDB server
// use just "mongodb" if you connect to on-premise MongoDB server
client, err := mongo.NewClient("mongodb+srv://USER:PASSWORD@SERVER/experiments")
if err != nil {
log.Fatalf("failed to create new MongoDB client: %#v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Connect client
if err = client.Connect(ctx); err != nil {
log.Fatalf("failed to connect to MongoDB: %#v", err)
}
log.Printf("connected successfully")
// Get collection from database
coll := client.Database("experiments").Collection("proto")
// Create protobuf Timestamp value from golang Time
t := time.Now()
ts, err := ptypes.TimestampProto(t)
if err != nil {
log.Fatalf("failed to convert golang Time to protobuf Timestamp: %#v", err)
}
// Fill in data structure
in := Data{
BoolValue: true,
Int64Value: 12345,
DoubleValue: 123.45,
StringValue: "qwerty",
TimestampValue: ts,
BoolWrappedValue: &wrappers.BoolValue{Value: true},
Int64WrappedValue: &wrappers.Int64Value{Value: 12345},
DoubleWrappedValue: &wrappers.DoubleValue{Value: 123.45},
StringWrappedValue: &wrappers.StringValue{Value: "qwerty"},
}
log.Printf("insert data into collection <experiments.proto>...")
// Insert data into the collection
res, err := coll.InsertOne(ctx, &in)
if err != nil {
log.Fatalf("insert data into collection <experiments.proto>: %#v", err)
}
id := res.InsertedID
log.Printf("inserted new item with id=%v successfully", id)
// Create filter and output structure to read data from collection
var out Data
filter := bson.D{{Key: "_id", Value: id}}
// Read data from collection
err = coll.FindOne(ctx, filter).Decode(&out)
if err != nil {
log.Fatalf("failed to read data (id=%v) from collection <experiments.proto>: %#v", id, err)
}
log.Print("read successfully")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment