|
@@ -28,6 +28,18 @@ func NewMgo(addr, db string, size int) *MongodbSim {
|
|
return mgo
|
|
return mgo
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func NewMgoWithUser(addr, db, uname, upwd string, size int) *MongodbSim {
|
|
|
|
+ mgo := &MongodbSim{
|
|
|
|
+ MongodbAddr: addr,
|
|
|
|
+ Size: size,
|
|
|
|
+ DbName: db,
|
|
|
|
+ UserName: uname,
|
|
|
|
+ Password: upwd,
|
|
|
|
+ }
|
|
|
|
+ mgo.InitPool()
|
|
|
|
+ return mgo
|
|
|
|
+}
|
|
|
|
+
|
|
type Bluk struct {
|
|
type Bluk struct {
|
|
ms *MgoSess
|
|
ms *MgoSess
|
|
writes []mongo.WriteModel
|
|
writes []mongo.WriteModel
|
|
@@ -135,7 +147,6 @@ type MgoSess struct {
|
|
fields interface{}
|
|
fields interface{}
|
|
limit int64
|
|
limit int64
|
|
skip int64
|
|
skip int64
|
|
- pipe []map[string]interface{}
|
|
|
|
all interface{}
|
|
all interface{}
|
|
M *MongodbSim
|
|
M *MongodbSim
|
|
}
|
|
}
|
|
@@ -178,9 +189,12 @@ func (ms *MgoSess) Sort(sorts ...string) *MgoSess {
|
|
ms.sorts = sorts
|
|
ms.sorts = sorts
|
|
return ms
|
|
return ms
|
|
}
|
|
}
|
|
-func (ms *MgoSess) Pipe(p []map[string]interface{}) *MgoSess {
|
|
|
|
- ms.pipe = p
|
|
|
|
- return ms
|
|
|
|
|
|
+func (ms *MgoSess) Pipe(p []map[string]interface{}) *pipe {
|
|
|
|
+ pe := &pipe{
|
|
|
|
+ ms: ms,
|
|
|
|
+ pipeline: p,
|
|
|
|
+ }
|
|
|
|
+ return pe
|
|
}
|
|
}
|
|
func (ms *MgoSess) Insert(doc interface{}) error {
|
|
func (ms *MgoSess) Insert(doc interface{}) error {
|
|
_, err := ms.M.C.Database(ms.db).Collection(ms.coll).InsertOne(ms.M.Ctx, doc)
|
|
_, err := ms.M.C.Database(ms.db).Collection(ms.coll).InsertOne(ms.M.Ctx, doc)
|
|
@@ -235,53 +249,58 @@ func (ms *MgoSess) One(v *map[string]interface{}) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
func (ms *MgoSess) All(v *[]map[string]interface{}) {
|
|
func (ms *MgoSess) All(v *[]map[string]interface{}) {
|
|
- cur, err := ms.M.C.Database(ms.db).Collection(ms.coll).Aggregate(ms.M.Ctx, ms.pipe)
|
|
|
|
|
|
+ of := options.Find()
|
|
|
|
+ if ms.fields != nil {
|
|
|
|
+ of.SetProjection(ms.fields)
|
|
|
|
+ }
|
|
|
|
+ if len(ms.sorts) > 0 {
|
|
|
|
+ of.SetSort(ms.toSort())
|
|
|
|
+ }
|
|
|
|
+ if ms.skip > 0 {
|
|
|
|
+ of.SetSkip(ms.skip)
|
|
|
|
+ }
|
|
|
|
+ if ms.limit > 0 {
|
|
|
|
+ of.SetLimit(ms.limit)
|
|
|
|
+ }
|
|
|
|
+ cur, err := ms.M.C.Database(ms.db).Collection(ms.coll).Find(ms.M.Ctx, ms.query, of)
|
|
if err == nil && cur.Err() == nil {
|
|
if err == nil && cur.Err() == nil {
|
|
cur.All(ms.M.Ctx, v)
|
|
cur.All(ms.M.Ctx, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+func (ms *MgoSess) toSort() bson.D {
|
|
|
|
+ sort := bson.D{}
|
|
|
|
+ for _, k := range ms.sorts {
|
|
|
|
+ switch k[:1] {
|
|
|
|
+ case "-":
|
|
|
|
+ sort = append(sort, bson.E{k[1:], -1})
|
|
|
|
+ case "+":
|
|
|
|
+ sort = append(sort, bson.E{k[1:], 1})
|
|
|
|
+ default:
|
|
|
|
+ sort = append(sort, bson.E{k, 1})
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return sort
|
|
|
|
+}
|
|
func (ms *MgoSess) Iter() *MgoIter {
|
|
func (ms *MgoSess) Iter() *MgoIter {
|
|
it := &MgoIter{}
|
|
it := &MgoIter{}
|
|
coll := ms.M.C.Database(ms.db).Collection(ms.coll)
|
|
coll := ms.M.C.Database(ms.db).Collection(ms.coll)
|
|
- var cur *mongo.Cursor
|
|
|
|
- var err error
|
|
|
|
- if ms.query != nil {
|
|
|
|
- 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.D{}
|
|
|
|
- for _, k := range ms.sorts {
|
|
|
|
- switch k[:1] {
|
|
|
|
- case "-":
|
|
|
|
- sort = append(sort, bson.E{k[1:], -1})
|
|
|
|
- case "+":
|
|
|
|
- sort = append(sort, bson.E{k[1:], 1})
|
|
|
|
- default:
|
|
|
|
- sort = append(sort, bson.E{k, 1})
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- find.SetSort(sort)
|
|
|
|
- }
|
|
|
|
- if ms.fields != nil {
|
|
|
|
- find.SetProjection(ms.fields)
|
|
|
|
- }
|
|
|
|
- cur, err = coll.Find(ms.M.Ctx, ms.query, find)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Println("mgo find err", err.Error())
|
|
|
|
- }
|
|
|
|
- } else if ms.pipe != nil {
|
|
|
|
- aggregate := options.Aggregate()
|
|
|
|
- aggregate.SetBatchSize(100)
|
|
|
|
- cur, err = coll.Aggregate(ms.M.Ctx, ms.pipe, aggregate)
|
|
|
|
- if err != nil {
|
|
|
|
- log.Println("mgo aggregate err", err.Error())
|
|
|
|
- }
|
|
|
|
|
|
+ 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 {
|
|
|
|
+ find.SetSort(ms.toSort())
|
|
|
|
+ }
|
|
|
|
+ if ms.fields != nil {
|
|
|
|
+ find.SetProjection(ms.fields)
|
|
|
|
+ }
|
|
|
|
+ cur, err := coll.Find(ms.M.Ctx, ms.query, find)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("mgo find err", err.Error())
|
|
}
|
|
}
|
|
if err == nil {
|
|
if err == nil {
|
|
it.Cursor = cur
|
|
it.Cursor = cur
|
|
@@ -290,6 +309,33 @@ func (ms *MgoSess) Iter() *MgoIter {
|
|
return it
|
|
return it
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+type pipe struct {
|
|
|
|
+ ms *MgoSess
|
|
|
|
+ pipeline []map[string]interface{}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (p *pipe) All(v *[]map[string]interface{}) {
|
|
|
|
+ cur, err := p.ms.M.C.Database(p.ms.db).Collection(p.ms.coll).Aggregate(p.ms.M.Ctx, p.pipeline)
|
|
|
|
+ if err == nil && cur.Err() == nil {
|
|
|
|
+ cur.All(p.ms.M.Ctx, v)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+func (p *pipe) Iter() *MgoIter {
|
|
|
|
+ it := &MgoIter{}
|
|
|
|
+ coll := p.ms.M.C.Database(p.ms.db).Collection(p.ms.coll)
|
|
|
|
+ aggregate := options.Aggregate()
|
|
|
|
+ aggregate.SetBatchSize(100)
|
|
|
|
+ cur, err := coll.Aggregate(p.ms.M.Ctx, p.pipeline, aggregate)
|
|
|
|
+ if err != nil {
|
|
|
|
+ log.Println("mgo aggregate err", err.Error())
|
|
|
|
+ }
|
|
|
|
+ if err == nil {
|
|
|
|
+ it.Cursor = cur
|
|
|
|
+ it.Ctx = p.ms.M.Ctx
|
|
|
|
+ }
|
|
|
|
+ return it
|
|
|
|
+}
|
|
|
|
+
|
|
type MongodbSim struct {
|
|
type MongodbSim struct {
|
|
MongodbAddr string
|
|
MongodbAddr string
|
|
Size int
|
|
Size int
|
|
@@ -365,6 +411,21 @@ func (m *MongodbSim) Close() {
|
|
<-m.pool
|
|
<-m.pool
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+//新建表并生成索引
|
|
|
|
+func (m *MongodbSim) CreateIndex(c string, models []mongo.IndexModel) bool {
|
|
|
|
+ defer catch()
|
|
|
|
+ m.Open()
|
|
|
|
+ defer m.Close()
|
|
|
|
+ coll := m.C.Database(m.DbName).Collection(c)
|
|
|
|
+ names, err := coll.Indexes().CreateMany(m.Ctx, models)
|
|
|
|
+ if err == nil && len(names) > 0 {
|
|
|
|
+ return true
|
|
|
|
+ } else {
|
|
|
|
+ log.Println("CreateIndex Error:", err)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func (m *MongodbSim) Save(c string, doc interface{}) string {
|
|
func (m *MongodbSim) Save(c string, doc interface{}) string {
|
|
defer catch()
|
|
defer catch()
|
|
m.Open()
|
|
m.Open()
|
|
@@ -781,7 +842,7 @@ func ToObjectIds(ids []string) []primitive.ObjectID {
|
|
|
|
|
|
//自动添加更新时间
|
|
//自动添加更新时间
|
|
func autoUpdateTime(db, coll string, ue *bson.M) {
|
|
func autoUpdateTime(db, coll string, ue *bson.M) {
|
|
- if db == "qfw" && coll == "user" {
|
|
|
|
|
|
+ if coll == "user" {
|
|
set := ObjToM((*ue)["$set"])
|
|
set := ObjToM((*ue)["$set"])
|
|
if *set == nil {
|
|
if *set == nil {
|
|
set = &bson.M{}
|
|
set = &bson.M{}
|
|
@@ -790,3 +851,11 @@ func autoUpdateTime(db, coll string, ue *bson.M) {
|
|
(*ue)["$set"] = set
|
|
(*ue)["$set"] = set
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func IsObjectIdHex(hex string) bool {
|
|
|
|
+ _, err := primitive.ObjectIDFromHex(hex)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+}
|