Categorygithub.com/softwok/mongo-util
module
0.6.0
Repository: https://github.com/softwok/mongo-util.git
Documentation: pkg.go.dev

# README

Introduction

Wrapper and Util for MongoDB driver to perform CRUD operations.

CI

Mongo reference documentation: https://www.mongodb.com/docs/manual/reference

Initialization

err := mdu.Init(
		&mdu.Config{CtxTimeout: 5 * time.Second},
		"mango_test_db",
		options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
    panic(err)
}

MongoDB Collection Definition

By adding mdu.DefaultModel in model will include following attributes and values are generated automatically:

  • Mongo Object ID:
    • ID primitive.ObjectID `json:"id" bson:"_id,omitempty"
  • Created Date:
    • CreatedAt time.Time `json:"created_at" bson:"created_at"
  • Updated Date:
    • UpdatedAt time.Time `json:"updated_at" bson:"updated_at"

Example Model:

type product struct {
	mdu.DefaultModel `bson:",inline"`
	Name             string `json:"name" bson:"name"`
	Price            int    `json:"price" bson:"price"`
}

Create

testProduct := &product{
    Name:  "Test Create",
    Price: 100,
}
productsColl := mdu.Coll(testProduct)
id, err := productsColl.Create(mdu.Ctx(), testProduct)

Update

testProduct.Name = "Test Update"
productsColl := mdu.Coll(testProduct)
err := productsColl.Update(mdu.Ctx(), testProduct)

Find

productsColl := mdu.Coll(&product{})
err := productsColl.FindByID(mdu.Ctx(), id, testProduct)

Delete

productsColl := mdu.Coll(&product{})
err = productsColl.Delete(mdu.Ctx(), testProduct)

FindAll

productsColl := mdu.Coll(&product{})
var results []product
err := productsColl.FindAll(mdu.Ctx(), &results, bson.D{})

APIs

  • FindByID: FindByID method finds a doc and decodes it to a model, otherwise returns an error.
  • First: First method searches and returns the first document in the search results.
  • Create: Create method inserts a new model into the database.
  • Update: Update function persists the changes made to a model to the database using the specified context.
  • Delete: Delete method deletes a model (doc) from a collection using the specified context.
  • FindAll: FindAll finds, decodes and returns the results using the specified context.

# Packages

Package builder help us to write aggregates, filters, update maps simpler.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author