Blame view

common/db/mongoproxy/mongo.go 2.55 KB
436e0af4   zhangqijia   reactor: dir; Ac...
1
  package mongoproxy
765431a4   zhangqijia   增加schema接口, 抽象 mo...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
  import (
  	"context"
  	"fmt"
  	"go.mongodb.org/mongo-driver/bson"
  	"go.mongodb.org/mongo-driver/mongo"
  	"go.mongodb.org/mongo-driver/mongo/options"
  	"go.mongodb.org/mongo-driver/x/bsonx"
  	"pro2d/common/components"
  	"sort"
  	"strings"
  )
  
  var (
  	mongoClient *mongo.Client
  	mongoDatabase *mongo.Database
  )
  
  type MgoColl struct {
  	components.IDB
  	Schema components.ISchema
  
  	dbname string
  	coll *mongo.Collection
  }
  
  func NewMongoColl(dbname string, schema components.ISchema) *MgoColl {
  	m := &MgoColl{
  		dbname: dbname,
436e0af4   zhangqijia   reactor: dir; Ac...
31
  		coll:   DB().Collection(dbname),
765431a4   zhangqijia   增加schema接口, 抽象 mo...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  		Schema: schema,
  	}
  	return m
  }
  
  func (m *MgoColl) CreateTable() error {
  	colls, _ := DB().ListCollectionNames(context.TODO(), bson.D{})
  	pos := sort.SearchStrings(colls, m.dbname)
  	if pos != len(colls) {
  		if m.dbname == colls[pos] {
  			return DB().CreateCollection(context.TODO(), m.dbname)
  		}
  	}
  	return DB().CreateCollection(context.TODO(), m.dbname)
  }
  
  func (m *MgoColl) Create() (interface{}, error){
  	return m.coll.InsertOne(context.TODO(), m.Schema.GetSchema())
  }
  
8d983031   zhangqijia   loginReq uid -> t...
52
53
54
55
56
57
58
59
  func (m *MgoColl) Save() error{
  	_, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), m.Schema.GetSchema())
  	if err != nil {
  		return err
  	}
  	return nil
  }
  
765431a4   zhangqijia   增加schema接口, 抽象 mo...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  func (m *MgoColl) Load() error{
  	r := m.coll.FindOne(context.TODO(), m.Schema.GetPri())
  	err := r.Decode(m.Schema.GetSchema())
  	if err != nil {
  		return err
  	}
  	return nil
  }
  
  // 查询单个
  func (m *MgoColl) FindOne() error {
  	singleResult := m.coll.FindOne(context.TODO(), m.Schema.GetPri())
  	return singleResult.Decode(m.Schema.GetSchema())
  }
  
  func (m *MgoColl) UpdateOne(filter interface{}, update interface{})*mongo.UpdateResult {
  	res, err := m.coll.UpdateOne(context.TODO(), filter, bson.D{{"$set", update}})
  	if err != nil {
  		return nil
  	}
  	return res
  }
  
  func (m *MgoColl) UpdateProperty(key string, val interface{}) error {
  	_, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), bson.D{{"$set", bson.M{strings.ToLower(key): val}}})
  	return err
  }
  
  func (m *MgoColl) UpdateProperties(properties map[string]interface{}) error {
  	_, err := m.coll.UpdateOne(context.TODO(), m.Schema.GetPri(), properties)
  	return err
  }
  
  //索引
  func (m *MgoColl) SetUnique(key string) (string, error){
  	return m.coll.Indexes().CreateOne(
  		context.Background(),
  		mongo.IndexModel{
  			Keys   : bsonx.Doc{{key, bsonx.Int32(1)}},
  			Options: options.Index().SetUnique(true),
  		},
  	)
  }
  
  func (m *MgoColl) Delete(key string, value interface{}) int64 {
  	filter := bson.D{ {key, value}}
  	count, err := m.coll.DeleteOne(context.TODO(), filter, nil)
  	if err != nil {
  		fmt.Println(err)
  	}
  	return count.DeletedCount
  
  }