123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- package main
- import (
- "context"
- "log"
- "strings"
- "time"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- )
- type MgoSess struct {
- Db string
- Coll string
- Query interface{}
- Sorts []string
- fields interface{}
- limit int64
- skip int64
- M *MongodbSim
- }
- type MgoIter struct {
- Cursor *mongo.Cursor
- }
- func NewMgo(addr, db string, size int) *MongodbSim {
- mgo := &MongodbSim{
- MongodbAddr: addr,
- Size: size,
- DbName: db,
- }
- mgo.InitPool()
- return mgo
- }
- func (mt *MgoIter) Next(result interface{}) bool {
- if mt.Cursor != nil {
- if mt.Cursor.Next(nil) {
- err := mt.Cursor.Decode(result)
- if err != nil {
- log.Println("mgo cur err", err.Error())
- mt.Cursor.Close(nil)
- return false
- }
- return true
- } else {
- mt.Cursor.Close(nil)
- return false
- }
- } else {
- return false
- }
- }
- func (ms *MgoSess) DB(name string) *MgoSess {
- ms.Db = name
- return ms
- }
- func (ms *MgoSess) C(name string) *MgoSess {
- ms.Coll = name
- return ms
- }
- func (ms *MgoSess) Find(q interface{}) *MgoSess {
- ms.Query = q
- return ms
- }
- func (ms *MgoSess) Select(fields interface{}) *MgoSess {
- ms.fields = fields
- return ms
- }
- func (ms *MgoSess) Limit(limit int64) *MgoSess {
- ms.limit = limit
- return ms
- }
- func (ms *MgoSess) Skip(skip int64) *MgoSess {
- ms.skip = skip
- return ms
- }
- func (ms *MgoSess) Sort(sorts ...string) *MgoSess {
- ms.Sorts = sorts
- return ms
- }
- func (ms *MgoSess) Iter() *MgoIter {
- it := &MgoIter{}
- find := options.Find()
- if ms.skip > 0 {
- find.SetSkip(ms.skip)
- }
- if ms.limit > 0 {
- find.SetLimit(ms.limit)
- }
- find.SetBatchSize(100)
- if len(ms.Sorts) > 0 {
- sort := bson.M{}
- for _, k := range ms.Sorts {
- switch k[:1] {
- case "-":
- sort[k[1:]] = -1
- case "+":
- sort[k[1:]] = 1
- default:
- sort[k] = 1
- }
- }
- find.SetSort(sort)
- }
- if ms.fields != nil {
- find.SetProjection(ms.fields)
- }
- cur, err := ms.M.C.Database(ms.Db).Collection(ms.Coll).Find(ms.M.Ctx, ms.Query, find)
- if err != nil {
- log.Println("mgo find err", err.Error())
- } else {
- it.Cursor = cur
- }
- return it
- }
- type MongodbSim struct {
- MongodbAddr string
- Size int
- // MinSize int
- DbName string
- C *mongo.Client
- Ctx context.Context
- ShortCtx context.Context
- pool chan bool
- }
- func (m *MongodbSim) GetMgoConn() *MgoSess {
- //m.Open()
- ms := &MgoSess{}
- ms.M = m
- return ms
- }
- func (m *MongodbSim) DestoryMongoConn(ms *MgoSess) {
- //m.Close()
- ms.M = nil
- ms = nil
- }
- func (m *MongodbSim) Destroy() {
- //m.Close()
- m.C.Disconnect(nil)
- m.C = nil
- }
- func (m *MongodbSim) InitPool() {
- opts := options.Client()
- opts.SetConnectTimeout(3 * time.Second)
- opts.ApplyURI("mongodb://" + m.MongodbAddr)
- opts.SetMaxPoolSize(uint64(m.Size))
- m.pool = make(chan bool, m.Size)
- opts.SetMaxConnIdleTime(2 * time.Hour)
- m.Ctx, _ = context.WithTimeout(context.Background(), 99999*time.Hour)
- m.ShortCtx, _ = context.WithTimeout(context.Background(), 1*time.Minute)
- client, err := mongo.Connect(m.ShortCtx, opts)
- if err != nil {
- log.Println("mgo init error:", err.Error())
- } else {
- m.C = client
- }
- }
- func (m *MongodbSim) Open() {
- m.pool <- true
- }
- func (m *MongodbSim) Close() {
- <-m.pool
- }
- //批量插入
- func (m *MongodbSim) UpSertBulk(c string, doc ...[]map[string]interface{}) bool {
- m.Open()
- defer m.Close()
- coll := m.C.Database(m.DbName).Collection(c)
- var writes []mongo.WriteModel
- for _, d := range doc {
- write := mongo.NewUpdateOneModel()
- write.SetFilter(d[0])
- write.SetUpdate(d[1])
- write.SetUpsert(true)
- writes = append(writes, write)
- }
- br, e := coll.BulkWrite(m.Ctx, writes)
- if e != nil {
- log.Println("mgo upsert error:", e.Error())
- return br == nil || br.UpsertedCount == 0
- }
- // else {
- // if r.UpsertedCount != int64(len(doc)) {
- // log.Println("mgo upsert uncomplete:uc/dc", r.UpsertedCount, len(doc))
- // }
- // return true
- // }
- return true
- }
- //批量插入
- func (m *MongodbSim) SaveBulk(c string, doc ...map[string]interface{}) bool {
- m.Open()
- defer m.Close()
- coll := m.C.Database(m.DbName).Collection(c)
- var writes []mongo.WriteModel
- for _, d := range doc {
- write := mongo.NewInsertOneModel()
- write.SetDocument(d)
- writes = append(writes, write)
- }
- br, e := coll.BulkWrite(m.Ctx, writes)
- if e != nil {
- b := strings.Index(e.Error(), "duplicate") > -1
- log.Println("mgo savebulk error:", e.Error())
- if br != nil {
- log.Println("mgo savebulk size:", br.InsertedCount)
- }
- return b
- }
- return true
- }
- func StringTOBsonId(id string) primitive.ObjectID {
- objectId, _ := primitive.ObjectIDFromHex(id)
- return objectId
- }
- //按条件统计
- func (m *MongodbSim) Count(c string, q interface{}) int64 {
- m.Open()
- defer m.Close()
- ct := options.Count()
- ct.SetMaxTime(180 * time.Second)
- res, err := m.C.Database(m.DbName).Collection(c).CountDocuments(m.Ctx, q, ct)
- if err != nil {
- log.Println("统计错误", err.Error())
- }
- return res
- }
- //按条件删除
- func (m *MongodbSim) Delete(c string, q interface{}) int64 {
- m.Open()
- defer m.Close()
- ct := options.Delete()
- res, err := m.C.Database(m.DbName).Collection(c).DeleteMany(m.Ctx, q, ct)
- if err != nil && res == nil {
- log.Println("删除错误", err.Error())
- }
- return res.DeletedCount
- }
- //按条件更新
- func (m *MongodbSim) Update(c string, q, u interface{}) int64 {
- m.Open()
- defer m.Close()
- ct := options.Update()
- res, err := m.C.Database(m.DbName).Collection(c).UpdateMany(m.Ctx, q, u, ct)
- if err != nil && res == nil {
- log.Println("删除错误", err.Error())
- }
- return res.ModifiedCount
- }
- //查找一条数据
- func (m *MongodbSim) FindOne(c string, q, fields interface{}, sorts []string) (res map[string]interface{}) {
- m.Open()
- defer m.Close()
- of := options.FindOne()
- if fields != nil {
- of.SetProjection(fields)
- }
- if len(sorts) > 0 {
- sort := bson.M{}
- for _, k := range sorts {
- switch k[:1] {
- case "-":
- sort[k[1:]] = -1
- case "+":
- sort[k[1:]] = 1
- default:
- sort[k] = 1
- }
- }
- of.SetSort(sort)
- }
- sr := m.C.Database(m.DbName).Collection(c).FindOne(m.Ctx, q, of)
- sr.Decode(&res)
- return
- }
|