1 Star 0 Fork 0

liuxuezhan / mylib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oldmongo.go 3.79 KB
一键复制 编辑 原始数据 按行查看 历史
liuxuezhan 提交于 2020-10-24 18:08 . 'new mongo'
package db
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/x/bsonx"
"gopkg.in/mgo.v2/bson"
"strings"
"time"
)
type Db_mongo struct {
client *mongo.Client
db *mongo.Database
Tables map[string]*mongo.Collection
}
//UseSession 使用事务
func (obj *Db_mongo) UseSession(ctx context.Context, fun func(mongo.SessionContext) error) error {
if ctx == nil {
ctx = context.Background()
}
return obj.client.UseSessionWithOptions(ctx,
options.Session().SetDefaultReadConcern(readconcern.Majority()),
func(ctx mongo.SessionContext) error {
ctx.StartTransaction()
if err := fun(ctx); err != nil {
return err
}
return ctx.CommitTransaction(ctx)
},
)
}
func (object *Db_mongo) Open(collectString, DBName string) {
var err error
object.client, err = mongo.NewClient(options.Client().ApplyURI(collectString))
if err != nil {
return
}
err = object.client.Connect(nil)
if err != nil {
}
object.db = object.client.Database(DBName)
object.Tables = make(map[string]*mongo.Collection)
}
func (obj *Db_mongo) Close() {
obj.client.Disconnect(nil)
}
//创建索引
func (obj *Db_mongo) Index(collectionName string, keys ...string) {
db := obj.Tables[collectionName]
if db == nil {
db = obj.db.Collection(collectionName)
}
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
indexView := db.Indexes()
keysDoc := bsonx.Doc{}
// 复合索引
for _, key := range keys {
if strings.HasPrefix(key, "-") {
keysDoc = keysDoc.Append(strings.TrimLeft(key, "-"), bsonx.Int32(-1))
} else {
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
}
}
// 创建索引
result, err := indexView.CreateOne(
context.Background(),
mongo.IndexModel{
Keys: keysDoc,
Options: options.Index().SetUnique(true),
},
opts,
)
if result == "" || err != nil {
}
}
//获取
func (obj *Db_mongo) Find(collectionName string, query, res []interface{}) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
db := obj.Tables[collectionName]
if db == nil {
db = obj.db.Collection(collectionName)
}
opts := options.Find().SetSort(bsonx.Doc{{"vote_count", bsonx.Int32(-1)}})
cursor, err := db.Find(ctx, bson.D{}, opts)
if err != nil {
}
for cursor.Next(context.Background()) {
var node interface{}
if err = cursor.Decode(&node); err != nil {
} else {
res = append(res, node)
}
}
cursor.Close(context.Background())
}
//通过id更新或插入
func (obj *Db_mongo) Write(table string, id, update interface{}) error {
update = bson.M{"$set": update}
updateOpts := options.Update().SetUpsert(true)
db := obj.db.Collection(table)
_, err := db.UpdateOne(context.Background(), bson.M{"_id": id}, update, updateOpts)
return err
}
//删除文档
func (obj *Db_mongo) Delete(table string, id interface{}) error {
db := obj.Tables[table]
if db == nil {
db = obj.db.Collection(table)
}
_, err := db.DeleteOne(context.Background(), bson.M{"_id": id})
return err
}
//获取迭代器
func (obj *Db_mongo) Foreach(table string, query interface{}, sort string, doc interface{}, callback func(error, bool, interface{})) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
db := obj.Tables[table]
if db == nil {
db = obj.db.Collection(table)
}
opts := options.Find().SetSort(bsonx.Doc{{"vote_count", bsonx.Int32(-1)}})
cursor, err := db.Find(ctx, bson.D{}, opts)
if err != nil {
}
for cursor.Next(context.Background()) {
var node interface{}
if err = cursor.Decode(&node); err != nil {
} else {
callback(nil, true, node)
}
}
cursor.Close(context.Background())
}
func (object *Db_mongo) MapReduce(collectionName string, query, job, res interface{}) error {
var ret error = nil
return ret
}
1
https://gitee.com/liuxuezhan/mylib.git
git@gitee.com:liuxuezhan/mylib.git
liuxuezhan
mylib
mylib
v1.1.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891