2022-08-31 11:10:08 +00:00
|
|
|
package libs
|
2022-08-29 19:30:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
. "code.hoteas.com/golang/hotime/common"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
type MongoDb struct {
|
2022-08-29 19:30:27 +00:00
|
|
|
Client *mongo.Client
|
|
|
|
Ctx context.Context
|
|
|
|
DataBase *mongo.Database
|
|
|
|
Connect *mongo.Collection
|
|
|
|
LastErr error
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func GetMongoDb(database, url string) (*MongoDb, error) {
|
|
|
|
db := MongoDb{}
|
2022-08-29 19:30:27 +00:00
|
|
|
clientOptions := options.Client().ApplyURI(url)
|
|
|
|
|
|
|
|
db.Ctx = context.TODO()
|
2022-08-31 11:10:08 +00:00
|
|
|
// Connect to MongoDb
|
2022-08-29 19:30:27 +00:00
|
|
|
var err error
|
|
|
|
db.Client, err = mongo.Connect(db.Ctx, clientOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Check the connection
|
|
|
|
err = db.Client.Ping(db.Ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-31 11:10:08 +00:00
|
|
|
fmt.Println("Connected to MongoDb!")
|
2022-08-29 19:30:27 +00:00
|
|
|
//databases, err := db.Client.ListDatabaseNames(db.Ctx, bson.M{})
|
|
|
|
//if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
//}
|
|
|
|
//fmt.Println(databases)
|
|
|
|
db.DataBase = db.Client.Database(database)
|
|
|
|
return &db, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func (that *MongoDb) Insert(table string, data interface{}) string {
|
2022-08-29 19:30:27 +00:00
|
|
|
collection := that.DataBase.Collection(table)
|
|
|
|
re, err := collection.InsertOne(that.Ctx, data)
|
|
|
|
if err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ObjToStr(re.InsertedID)
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func (that *MongoDb) InsertMany(table string, data ...interface{}) Slice {
|
2022-08-29 19:30:27 +00:00
|
|
|
collection := that.DataBase.Collection(table)
|
|
|
|
re, err := collection.InsertMany(that.Ctx, data)
|
|
|
|
if err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return Slice{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjToSlice(re.InsertedIDs)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func (that *MongoDb) Update(table string, data Map, where Map) int64 {
|
2022-08-29 20:44:37 +00:00
|
|
|
collection := that.DataBase.Collection(table)
|
|
|
|
re, err := collection.UpdateMany(that.Ctx, where, data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return re.ModifiedCount
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func (that *MongoDb) Delete(table string, where Map) int64 {
|
2022-08-29 20:44:37 +00:00
|
|
|
collection := that.DataBase.Collection(table)
|
|
|
|
re, err := collection.DeleteMany(that.Ctx, where)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return re.DeletedCount
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func (that *MongoDb) Get(table string, where Map) Map {
|
2022-08-29 19:30:27 +00:00
|
|
|
results := []Map{}
|
|
|
|
var cursor *mongo.Cursor
|
|
|
|
var err error
|
|
|
|
collection := that.DataBase.Collection(table)
|
|
|
|
if cursor, err = collection.Find(that.Ctx, where, options.Find().SetSkip(0), options.Find().SetLimit(2)); err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
//延迟关闭游标
|
|
|
|
defer func() {
|
|
|
|
if err := cursor.Close(that.Ctx); err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
//这里的结果遍历可以使用另外一种更方便的方式:
|
|
|
|
if err = cursor.All(that.Ctx, &results); err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-29 20:05:17 +00:00
|
|
|
if len(results) > 0 {
|
|
|
|
|
|
|
|
return results[0]
|
|
|
|
}
|
|
|
|
return nil
|
2022-08-29 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 11:10:08 +00:00
|
|
|
func (that MongoDb) Select(table string, where Map, page, pageRow int64) []Map {
|
2022-08-29 19:30:27 +00:00
|
|
|
page = (page - 1) * pageRow
|
|
|
|
if page < 0 {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
results := []Map{}
|
|
|
|
var cursor *mongo.Cursor
|
|
|
|
var err error
|
|
|
|
collection := that.DataBase.Collection(table)
|
|
|
|
if cursor, err = collection.Find(that.Ctx, where, options.Find().SetSkip(page), options.Find().SetLimit(pageRow)); err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
//延迟关闭游标
|
|
|
|
defer func() {
|
|
|
|
if err := cursor.Close(that.Ctx); err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
//这里的结果遍历可以使用另外一种更方便的方式:
|
|
|
|
|
|
|
|
if err = cursor.All(that.Ctx, &results); err != nil {
|
|
|
|
that.LastErr = err
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|