Skip to content

tungquach/gmob

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gmob

GoDoc Test Go Report Card

Gmob is simple util for MongoDB Go Driver to quickly build an unordered representation of a BSON document (M) from Map or Struct.

Install

go get -u github.com/tungquach/gmob

Usage

package main

import (
    ...
    github.com/tungquach/gmob
    ...
)

// Car example entity
type Car struct {
    ID string `bson:"_id"`
    Name string `bson:"name"`
    CreatedAt time.Time `bson:"createdAt"`
}

func main() {
    // connect to mongodb
    clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
    client, err := mongo.Connect(context.TODO(), clientOptions)
    if err != nil {
        log.Fatal(err)
    }

    // cars collection
    ctx := context.Background()
    collection := client.Database("test").Collection("cars")

    // find record
    filter, _ := gmob.Build(Car{Name: "BMW"})
    result := &Car{}
    err = collection.FindOne(ctx, filter).Decode(result)
    log.Printf("result %+v, err: %v", result, err)

    // update record
    setValues, _ := gmob.Build(Car{Name: "BMW 2020"})
    err := collection.FindOneAndUpdate(ctx, bson.M{"_id": "car001"},
        bson.M{"$set": setValues}).Err()
    log.Printf("err: %v", err)
}

About

Go MongoDB driver document builder

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages