124 lines
2.8 KiB
Go
124 lines
2.8 KiB
Go
package mongodb
|
|
|
|
import (
|
|
. "code.hoteas.com/golang/hotime/common"
|
|
"context"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type mongoDb struct {
|
|
Client *mongo.Client
|
|
Ctx context.Context
|
|
DataBase *mongo.Database
|
|
Connect *mongo.Collection
|
|
LastErr error
|
|
}
|
|
|
|
func Init(database, url string) (*mongoDb, error) {
|
|
db := mongoDb{}
|
|
clientOptions := options.Client().ApplyURI(url)
|
|
|
|
db.Ctx = context.TODO()
|
|
// Connect to MongoDB
|
|
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
|
|
}
|
|
fmt.Println("Connected to MongoDB!")
|
|
//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
|
|
|
|
}
|
|
|
|
func (that *mongoDb) Insert(table string, data interface{}) string {
|
|
collection := that.DataBase.Collection(table)
|
|
re, err := collection.InsertOne(that.Ctx, data)
|
|
if err != nil {
|
|
that.LastErr = err
|
|
return ""
|
|
}
|
|
return ObjToStr(re.InsertedID)
|
|
}
|
|
|
|
func (that *mongoDb) InsertMany(table string, data ...interface{}) Slice {
|
|
collection := that.DataBase.Collection(table)
|
|
re, err := collection.InsertMany(that.Ctx, data)
|
|
if err != nil {
|
|
that.LastErr = err
|
|
return Slice{}
|
|
}
|
|
|
|
return ObjToSlice(re.InsertedIDs)
|
|
|
|
}
|
|
|
|
func (that *mongoDb) Get(table string, where Map) Map {
|
|
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
|
|
}
|
|
if len(results) > 0 {
|
|
|
|
return results[0]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (that mongoDb) Select(table string, where Map, page, pageRow int64) []Map {
|
|
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
|
|
}
|