Skip to content

Instantly share code, notes, and snippets.

View wemgl's full-sized avatar

Wembley Leach wemgl

View GitHub Profile
var t todo
idDoc := bson.D{{"_id", objectIDS}}
err = db.Collection(collName).FindOne(ctx, idDoc).Decode(&t)
if err != nil {
return "", fmt.Errorf("updateTask: couldn't decode task from db: %v", err)
}
var task string
fmt.Println("old task:", t.Task)
fmt.Print("updated task: ")
for input.Scan() {
res, err := db.Collection(collName).InsertOne(ctx, bson.D{
{"task", t.Task},
{"createdAt", primitive.DateTime(timeMillis(t.CreatedAt))},
{"modifiedAt", primitive.DateTime(timeMillis(t.ModifiedAt))},
})
if err != nil {
return "", fmt.Errorf("createTask: task for to-do list couldn't be created: %v", err)
}
return res.InsertedID.(primitive.ObjectID).Hex(), nil
@wemgl
wemgl / configDB.go
Last active February 23, 2019 12:29
Configuration of the MongoDB Connection
func configDB(ctx context.Context) (*mongo.Database, error) {
uri := fmt.Sprintf(`mongodb://%s:%s@%s/%s`,
ctx.Value(usernameKey).(string),
ctx.Value(passwordKey).(string),
ctx.Value(hostKey).(string),
ctx.Value(databaseKey).(string),
)
client, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
return nil, fmt.Errorf("todo: couldn't connect to mongo: %v", err)
@wemgl
wemgl / main.go
Last active August 31, 2018 18:05
Connecting to a MongoDB Database from Go
// package declaration and imports omitted for brevity
type key string
const (
hostKey = key("hostKey")
usernameKey = key("usernameKey")
passwordKey = key("passwordKey")
databaseKey = key("databaseKey")
)