验证中...
Languages: Go
Categories: Web开发技术
Latest update 2019-05-07 16:31
gistfile1.txt
Raw Copy
package main
import (
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type DBOper struct {
// Go 里没有类,创建类使用结构体代替
Dbname string
Collection string
}
type User_bean struct {
User_name string
Pwd string
// 结构体内部添加映射字段
}
func connect(DBOper *DBOper) (*mgo.Session, *mgo.Collection) {
session, err := mgo.Dial("mongodb://xxxxxx:27017") //用户名密码连接方式: mongodb://user:pwd@xxxxxx:27017
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
return session, session.DB(DBOper.Dbname).C(DBOper.Collection)
}
func find_one(dbname string, collection string, query bson.M) (bean interface{}) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
err := db.Find(query).One(&bean)
if err != nil {
panic(err)
}
defer se.Close()
return bean
}
func find_all(dbname string, collection string, query bson.M) (bean []interface{}) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
err := db.Find(query).All(&bean)
if err != nil {
panic(err)
}
defer se.Close()
return bean
}
func insert_one(dbname string, collection string, doc interface{}) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
err := db.Insert(&doc)
if err != nil {
panic(err)
}
defer se.Close()
return
}
func update(dbname string, collection string, query bson.M, updater interface{}) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
err := db.Update(query, updater)
if err != nil {
panic(err)
}
defer se.Close()
return
}
func upsert(dbname string, collection string, query bson.M, updater interface{}) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
_, err := db.Upsert(query, updater)
if err != nil {
panic(err)
}
defer se.Close()
return
}
func update_all(dbname string, collection string, query bson.M, updater interface{}) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
_, err := db.UpdateAll(query, updater)
if err != nil {
panic(err)
}
defer se.Close()
return
}
func delete_one(dbname string, collection string, query bson.M) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
err := db.Remove(query)
if err != nil {
panic(err)
}
defer se.Close()
return
}
func delete_all(dbname string, collection string, query bson.M) {
dboper := new(DBOper)
dboper.Dbname = dbname
dboper.Collection = collection
se, db := connect(dboper)
_, err := db.RemoveAll(query)
if err != nil {
panic(err)
}
defer se.Close()
return
}
func main() {
//r := find_one("dbname", "tab_user", bson.M{})
//fmt.Println(r)
//user1 := User_bean{
// User_name: "test1",
//}
//insert_one("dbname", "tab_user", user1)
//user2 := User_bean{
// User_name: "test1",
//}
//update("dbname", "tab_user", bson.M{"user_name": "test"}, user2)
//delete_one("dbname", "tab_user", bson.M{"user_name":"003"})
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025