itrmg

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2020 License: MIT Imports: 7 Imported by: 1

README

itrmg The itrmg package is a simplified usage of MongoDB official library for Go.

Installation

go get -u github.com/itrepablik/itrmg

Usage

These are the various examples on how you can use the itrmg package for your next Go project with MongoDB database.

package main

import (
	"fmt"
	"time"

	"github.com/itrepablik/itrlog"
	"github.com/itrepablik/itrmg"
)

const (
	_dbConStr = "mongodb://username:password.@localhost:27017"
	_dbName   = "database_name"
	_collName = "collection_name"
)

// ClientMG set the MongoDB's client variable to be called globally across your Go's project
var ClientMG = itrmg.ClientMG

func main() {
	// Initialize the MongoDB connection
	ClientMG, err := itrmg.InitMG(_dbConStr)
	if err != nil {
		itrlog.Fatal(err)
	}

	// itrmg.FindOneByID usage: find single row by using object id in MongoDB collection
	data, err := itrmg.FindOneByID(_dbName, _collName, ClientMG, "5e2a59e3f1a9a91790a13c37")
	if err != nil {
		itrlog.Fatal(err)
	}

	// Get the specific column name with it's value
	fmt.Println("data: ", data["pc_name"])

	// itrmg.Find usage: use filter, sort order and row limit for your query.
	filter := itrmg.DP{"created_by": "politz", "status": "Used"} // constract your filter query here
	sortOrder := itrmg.DP{"pc_name": -1}                         // descending sort order

	results, err := itrmg.Find(_dbName, _collName, ClientMG, filter, sortOrder, 2)
	if err != nil {
		itrlog.Fatal(err)
	}

	// Iterate the 'results' from the itrmg.Find query
	for _, value := range results {
		fmt.Println(value["pc_name"])
	}

	// InsertOne usage: this will insert one row to your collection
	newRow := itrmg.DP{
		"pc_name":      "pc name 1234",
		"license":      "abc 123",
		"price":        23,
		"ip_address":   "123456",
		"created_by":   "politz",
		"created_date": time.Now(),
		"status":       "Available",
		"is_active":    true,
	}

	isInserted, err := itrmg.InsertOne(_dbName, _collName, ClientMG, newRow)
	if err != nil {
		itrlog.Error(err)
	}

	if isInserted {
		fmt.Println("New row has been inserted!")
	}

	// UpdateOne usage: update only one row to your MongoDB collection
	updateFilter := itrmg.DP{"license": "abc 123", "created_by": "politz"}
	updateRow := itrmg.DP{
		"pc_name":       "pc name 888",
		"license":       "abc 111",
		"price":         30,
		"ip_address":    "123456",
		"modified_by":   "politz",
		"modified_date": time.Now(),
	}

	isUpdated, err := itrmg.UpdateOne(_dbName, _collName, ClientMG, updateRow, updateFilter)
	if err != nil {
		itrlog.Error(err)
	}

	if isUpdated {
		fmt.Println("Row has been modified successfully!")
	}

	// UpdateOneByID usage: single row update by using object id
	objID := "5eab87c0fcde9804abc5fbc9"
	updateRow1 := itrmg.DP{
		"pc_name":       "pc name 000",
		"license":       "abc 000",
		"price":         0,
		"ip_address":    "123456",
		"modified_by":   "politz",
		"modified_date": time.Now(),
	}

	isUpdated1, err := itrmg.UpdateOneByID(_dbName, _collName, ClientMG, updateRow1, objID)
	if err != nil {
		itrlog.Error(err)
	}

	if isUpdated1 {
		fmt.Println("Row has been modified successfully!")
	}

	// DeleteOneByID usage: delete any single row permanently filtered by object id.
	rowObjID := "5eab87c0fcde9804abc5fbc9"
	isDeleted, err := itrmg.DeleteOneByID(_dbName, _collName, ClientMG, rowObjID)
	if err != nil {
		itrlog.Error(err)
	}

	if isDeleted {
		fmt.Println("Row has been deleted successfully!")
	}
	
	// IsExist usage: check if specific information found in certain collection
	filter = itrmg.DP{"created_by": "politz"}
	isFound, err := itrmg.IsExist(_dbName, _collName, ClientMG, filter)
	if err != nil {
		itrlog.Error(err)
	}
	if isFound {
		fmt.Println("Record found!")
	}
	
	// GetFieldValue usage: get the specific field value from a certain collection.
	filter := itrmg.DP{"pc_name": "PPP Name"}
	strVal, err := itrmg.GetFieldValue(_dbName, _collName, ClientMG, filter, "license")
	if err != nil {
		itrlog.Error(err)
	}
	fmt.Println("strVal: ", strVal)
}

Optionally, if you've a struct for your data structure, you can specify the object id as "itrmg.ObjID".

package models

import "github.com/itrepablik/itrmg"

// YourDataStruct is a collection of your own data structure here.
type YourDataStruct struct {
	ID       itrmg.ObjID `json:"_id" bson:"_id"`
	TypeName string      `json:"type_name" bson:"type_name"`
	IsActive bool        `json:"is_active" bson:"is_active"`
}

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClientMG *mongo.Client

ClientMG initialize the MongoDB's client's pointer.

Functions

func CountRows added in v1.0.4

func CountRows(dbName, collName string, client *mongo.Client, filter DP) (int64, error)

CountRows gets the total number of rows from a collection.

func DeleteOne added in v1.0.4

func DeleteOne(dbName, collName string, client *mongo.Client, filter DP) (bool, error)

DeleteOne delete any single row permanently from a collection.

func DeleteOneByID

func DeleteOneByID(dbName, collName string, client *mongo.Client, objID string) (bool, error)

DeleteOneByID delete any single row permanently filetered MongoDB object ID from a MongoDB collection.

func GetFieldValue added in v1.0.3

func GetFieldValue(dbName, collName string, client *mongo.Client, filter DP, bsonFieldName string) (string, error)

GetFieldValue gets the string value of any specific field name from a collection. e.g Struct field "PCName" then the bson field is "pc_name", the bson field name must be

func GetFieldValueByID added in v1.0.4

func GetFieldValueByID(dbName, collName string, client *mongo.Client, objID, bsonFieldName string) (string, error)

GetFieldValueByID gets the string value of any specific field filtered by object id from a collection.

func InitMG

func InitMG(dbConStr string) (*mongo.Client, error)

InitMG initializes the MongoDB connections.

func InsertOne

func InsertOne(dbName, collName string, client *mongo.Client, data DP) (bool, error)

InsertOne insert one row in MongoDB collection.

func IsExist added in v1.0.3

func IsExist(dbName, collName string, client *mongo.Client, filter DP) (bool, error)

IsExist find any single row from a specified collection.

func UpdateOne

func UpdateOne(dbName, collName string, client *mongo.Client, data DP, filter DP) (bool, error)

UpdateOne update a single row in MongoDB collection.

func UpdateOneByID

func UpdateOneByID(dbName, collName string, client *mongo.Client, data DP, objID string) (bool, error)

UpdateOneByID update a single row filtered by MongoDB object ID from a MongoDB collection.

Types

type DM added in v1.0.1

type DM []map[string]interface{}

DM type is a slice map container for collection results storage.

func Find

func Find(dbName, collName string, client *mongo.Client, filter DP, sortOrder DP, setLimit int64) (DM, error)

Find find a multiple rows filtered by MongoDB object ID from a collection.

type DP

type DP map[string]interface{}

DP type is a data parameters to be used as common map container for collection results or use as the filter parameters, etc.

func FindOne added in v1.0.4

func FindOne(dbName, collName string, client *mongo.Client, filter DP) (DP, error)

FindOne find a single row and retrieves all columns from a collection.

func FindOneByID

func FindOneByID(dbName, collName string, client *mongo.Client, objID string) (DP, error)

FindOneByID find a single row filtered by MongoDB object ID from a collection.

type MGC added in v1.0.4

type MGC *mongo.Client

MGC is a Mongo client type

type ObjID added in v1.0.4

type ObjID primitive.ObjectID

ObjID is the MongoDB BSON ObjectID type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL