代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。