GithubHelp home page GithubHelp logo

mongohelper's Introduction

Impl By Struct Model

	config.SetConfig(config.Config{
		MongoAddress: "mongodb://localhost:27018/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false",
		MongoDbName: "goex" ,
		}) // run once . In Main Func


	userModel := new(Model)
	db := mongoHelper.NewMongo(context.Background(),"users", userModel)
	_, err := db.FindOne(&bson.D{{Key: "username", Value: "admin"}, {Key: "password", Value: "1234"}})
	if err != nil {
		if err == mongo.ErrNoDocuments {
			fmt.Println("user not found ")
		} else {
			fmt.Println(err.Error())
		}
	}

	fmt.Println(userModel.ID.Hex())
	fmt.Println(userModel.Username)
	fmt.Println(userModel.Password)
	fmt.Println(userModel.Admin)

Impl by Map

	// Map 
	dbMap := mongoHelper.NewMongo(context.Background(),"users", map[string]interface{}{})
	userMap, err := dbMap.FindAll(&bson.D{})
	if err != nil {
		if err == mongo.ErrNoDocuments {
			fmt.Println("user not found ")
		} else {
			fmt.Println(err.Error())
		}
	}
	fmt.Println(len(userMap))
	for _,v:=range userMap {
		fmt.Println((*v)["_id"].(primitive.ObjectID).Hex())
		fmt.Println((*v)["username"])
		fmt.Println((*v)["password"])
		fmt.Println((*v)["admin"])
	}
Extend mongo Helper
type MongoHelper[T any] struct {
	mongoHelper.MongoContainer[T]
}

func (m *MongoHelper[T]) MyFindOne(query *bson.D, options ...*options.FindOneOptions) (*Model, error) {
	user, err := m.ConnectionManager(func(ctx context.Context, collection *mongo.Collection) (interface{}, error) {
		one := collection.FindOne(ctx, query, options...)
		err := one.Decode(m.Model)
		if err == nil {
			return m.Model, nil
		}
		return nil, err
	})
	if err != nil {
		return nil, err
	}
	return user.(*Model), nil
}

Test Of Extended MongoHelper

	// Extended Helper
	db2 := MyNewMongo(context.Background(), "users", &Model{})
	userModel2, err2 := db2.MyFindOne(&bson.D{{Key: "username", Value: "naser"}, {Key: "password", Value: "1234"}})
	if err2 != nil {
		if err2 == mongo.ErrNoDocuments {
			fmt.Println("user not found ")
			return 
		} else {
			fmt.Println(err.Error())
			return 
		}
	}

	fmt.Println(userModel2.ID.Hex())
	fmt.Println(userModel2.Username)
	fmt.Println(userModel2.Password)
	fmt.Println(userModel2.Admin)
	

mongohelper's People

Contributors

mehdi-shokohi avatar dozheiny avatar

Stargazers

Parsa Dadras avatar Perma avatar Hossein Erfani avatar  avatar

Watchers

James Cloos avatar  avatar Kostas Georgiou avatar

Forkers

dozheiny

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.