Git Product home page Git Product logo

mdb's Introduction

mdb

A rich mongodb driver based on mgo and auto refresh when "Closed explicitly" or "EOF"

feature

  • do not need copy := session.Clone; defter copy.Close();
  • use db instance in project
  • less tcp connections
  • auto refresh connections when connection is break
  • more simple

why this one

if you use copy := session.Clone; defter copy.Close(); copy.DB("dbname).C("col").Find(...)

you may got "Closed explicitly" or "EOF" when in high concurrency

quick start

type Person struct {
	Name string
	Phone string
}

func main() {
    //the test is default db
	db, err := mdb.Dial("mongodb://127.0.0.1:27017/test")
	if err != nil {
		panic(err)
	}
	defer db.Close()
  
	c := db.C("people")
	err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},&Person{"Cla", "+55 53 8402 8510"})
	if err != nil {
		log.Fatal(err)
	}
	
	result := Person{}
	err = c.Find(bson.M{"name": "Ale"}).One(&result)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Phone:", result.Phone)
}

when mongo connection string and database name is different?

use mongo-connection-string + &db={db_name} use to config your db name

example:

db, err := mdb.Dial("mongodb://username:[email protected]:27017?db=test")

when username is not an administrator

db, err := mdb.Dial("mongodb://username:[email protected]:27017/test")
//when you have to connect another db first
db, err := mdb.Dial("mongodb://username:[email protected]:27017/db_for_connect?db=test")

new connection string parameter

maxRetries : max retries time when network is error, default is 2

db : database name when your connection string and database name is different

FULL Example:

db, err := mdb.Dial("mongodb://username:[email protected]:27017?db=test&maxRetries=2")

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.