| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- package dao
- import (
- "context"
- "fmt"
- "github.com/gogf/gf/util/gconv"
- "gmanager/library/mongo/util"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- "strconv"
- "time"
- )
- type Mgo struct {
- database string
- collection string
- }
- func NewMgo(database, collection string) *Mgo {
- return &Mgo{
- database,
- collection,
- }
- }
- // 查询单个
- func (m *Mgo) FindOne(key string, value interface{}) *mongo.SingleResult {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- //collection.
- filter := bson.D{{key, value}}
- singleResult := collection.FindOne(context.TODO(), filter)
- return singleResult
- }
- // 查询单个
- func (m *Mgo) FindOne_ServerId(key string, value interface{}, serverId string) *mongo.SingleResult {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
- //collection.
- filter := bson.D{{key, value}}
- singleResult := collection.FindOne(context.TODO(), filter)
- return singleResult
- }
- func (m *Mgo) FindOneBson(filter bson.D) int64 {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- //collection.
- //filter := bson.D{{key, value}}
- singleResult, _ := collection.CountDocuments(context.TODO(), filter)
- return singleResult
- }
- func (m *Mgo) FindOneBson_ServerId(filter bson.D, serverId string) int64 {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
- singleResult, _ := collection.CountDocuments(context.TODO(), filter)
- return singleResult
- }
- //// 查询单个
- //func (m *Mgo) FindExit(key string, value interface{}) *mongo.SingleResult {
- // client := DB.Mongo
- // collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- // //collection.
- // filter := bson.D{{key, value}}
- // singleResult := collection.fub(context.TODO(), filter)
- // return singleResult
- //}
- // 查询多个
- func (m *Mgo) FindAll(key string, value interface{}) *mongo.Cursor {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- //collection.
- filter := bson.D{{key, value}}
- singleResult, _ := collection.Find(context.TODO(), filter)
- return singleResult
- }
- func (m *Mgo) FindAllBson(filter bson.D) *mongo.Cursor {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- //collection.
- //filter := bson.D{{key, value}}
- singleResult, _ := collection.Find(context.TODO(), filter)
- return singleResult
- }
- func (m *Mgo) FindAllBson_ServerId(filter bson.D, serverId string) *mongo.Cursor {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
- singleResult, _ := collection.Find(context.TODO(), filter)
- return singleResult
- }
- //插入单个
- func (m *Mgo) InsertOne(value interface{}) *mongo.InsertOneResult {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- insertResult, err := collection.InsertOne(context.TODO(), value)
- if err != nil {
- fmt.Println(err)
- }
- return insertResult
- }
- //插入单个
- func (m *Mgo) InsertOne_ServerId(value interface{}, serverId string) *mongo.InsertOneResult {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection + "_" + serverId)
- insertResult, err := collection.InsertOne(context.TODO(), value)
- if err != nil {
- fmt.Println(err)
- }
- return insertResult
- }
- func (m *Mgo) InsertOneByTime(value interface{}, logtime string) *mongo.InsertOneResult {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection + "_" + logtime)
- insertResult, err := collection.InsertOne(context.TODO(), value)
- if err != nil {
- fmt.Println(err)
- }
- return insertResult
- }
- //更新单个
- func (m *Mgo) UpdateOne(key primitive.ObjectID, value interface{}) int64 {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- filter := bson.D{{"_id", key}}
- update := bson.D{{"$set", value}}
- count, err := collection.UpdateOne(context.TODO(), filter, update)
- if err != nil {
- fmt.Println(err)
- }
- return count.UpsertedCount
- }
- func (m *Mgo) UpdateOneValue(key string, num int, itemyype string, serverid int) int64 {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- updateOpts := options.Update().SetUpsert(true)
- filter := bson.D{{"ItemName", key}, {"serverid", serverid}}
- update := bson.D{{"$set", bson.D{{"ItemName", key}, {"type", itemyype}}},
- {"$inc", bson.D{{"itemNum", num}}}}
- count, err := collection.UpdateOne(context.TODO(), filter, update, updateOpts)
- if err != nil {
- fmt.Println(err)
- }
- filter = bson.D{{"ItemName", key}, {"serverid", 0}}
- count, err = collection.UpdateOne(context.TODO(), filter, update, updateOpts)
- return count.UpsertedCount
- }
- func (m *Mgo) UpdateOneValueByTime(key string, num int, logtime string, itemyype string, serverid int) int64 {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection + "_" + logtime)
- updateOpts := options.Update().SetUpsert(true)
- filter := bson.D{{"ItemName", key}, {"serverid", serverid}}
- update := bson.D{{"$set", bson.D{{"ItemName", key}, {"type", itemyype}}},
- {"$inc", bson.D{{"itemNum", num}}}}
- count, err := collection.UpdateOne(context.TODO(), filter, update, updateOpts)
- if err != nil {
- fmt.Println(err)
- }
- filter = bson.D{{"ItemName", key}, {"serverid", 0}}
- count, err = collection.UpdateOne(context.TODO(), filter, update, updateOpts)
- return count.UpsertedCount
- }
- //查询集合里有多少数据
- func (m *Mgo) CollectionCount() (string, int64) {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- name := collection.Name()
- size, err := collection.EstimatedDocumentCount(context.TODO())
- if err != nil {
- fmt.Println(err)
- }
- return name, size
- }
- //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取
- func (m *Mgo) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- SORT := bson.D{{"_id", sort}} //filter := bson.D{{key,value}}
- filter := bson.D{{}}
- findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip)
- temp, _ := collection.Find(context.Background(), filter, findOptions)
- return temp
- }
- //获取集合创建时间和编号
- func (m *Mgo) ParsingId(result string) (time.Time, uint64) {
- temp1 := result[:8]
- timestamp, _ := strconv.ParseInt(temp1, 16, 64)
- dateTime := time.Unix(timestamp, 0) //这是截获情报时间 时间格式 2019-04-24 09:23:39 +0800 CST
- temp2 := result[18:]
- count, _ := strconv.ParseUint(temp2, 16, 64) //截获情报的编号
- return dateTime, count
- }
- //删除文章和查询文章
- func (m *Mgo) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- filter := bson.D{{key, value}}
- singleResult := collection.FindOne(context.TODO(), filter)
- DeleteResult, err := collection.DeleteOne(context.TODO(), filter, nil)
- if err != nil {
- fmt.Println("删除时出现错误,你删不掉的~")
- }
- return DeleteResult.DeletedCount, singleResult
- }
- //删除文章
- func (m *Mgo) Delete(key string, value interface{}) int64 {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- filter := bson.D{{key, value}}
- count, err := collection.DeleteOne(context.TODO(), filter, nil)
- if err != nil {
- fmt.Println(err)
- }
- return count.DeletedCount
- }
- //删除多个
- func (m *Mgo) DeleteMany(key string, value interface{}) int64 {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- filter := bson.D{{key, value}}
- count, err := collection.DeleteMany(context.TODO(), filter)
- if err != nil {
- fmt.Println(err)
- }
- return count.DeletedCount
- }
- func (m *Mgo) GetPageList(filter bson.D, page util.Page) *mongo.Cursor {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- SORT := bson.D{{"_id", -1}} //1:升序; -1:降序
- findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1)))
- singleResult, err := collection.Find(context.TODO(), filter, findOptions)
- if err != nil {
- fmt.Println(err)
- }
- return singleResult
- }
- func (m *Mgo) GetPageList_ServerId(filter bson.D, page util.Page, serverId string) *mongo.Cursor {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
- SORT := bson.D{{"_id", -1}} //1:升序; -1:降序
- findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1)))
- singleResult, err := collection.Find(context.TODO(), filter, findOptions)
- if err != nil {
- fmt.Println(err)
- }
- return singleResult
- }
- //查询数量
- func (m *Mgo) GetListCount(filter bson.D) int {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- singleResult, err := collection.Find(context.TODO(), filter)
- if err != nil {
- fmt.Println(err)
- }
- var showsWithInfo []bson.M
- if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil {
- fmt.Println(err)
- }
- return len(showsWithInfo)
- }
- //查询数量
- func (m *Mgo) GetListCountByServer(filter bson.D, serverId string) int {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
- singleResult, err := collection.Find(context.TODO(), filter)
- if err != nil {
- fmt.Println(err)
- }
- var showsWithInfo []bson.M
- if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil {
- fmt.Println(err)
- }
- return len(showsWithInfo)
- }
- //查询数量
- func (m *Mgo) GetListCountByServer2(filter bson.D, serverId string) int {
- client := DB.Mongo
- collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone()
- count, err := collection.CountDocuments(context.TODO(), filter)
- if err != nil {
- fmt.Println(err)
- }
- return int(count)
- }
- func (m *Mgo) InsUpdate(id string, state int) *mongo.UpdateResult {
- obj_id, _ := primitive.ObjectIDFromHex(id)
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- filter := bson.D{{"_id", obj_id}}
- update := bson.D{{"$set", bson.D{{"state", state}}}}
- updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
- fmt.Println(updateResult)
- if err != nil {
- fmt.Println(err)
- }
- return updateResult
- }
- // 参数类型不同
- func (m *Mgo) InsUpdate2(id int32, state string) *mongo.UpdateResult {
- client := DB.Mongo
- collection := client.Database(m.database).Collection(m.collection)
- filter := bson.D{{"_id", id}}
- update := bson.D{{"$set", bson.D{{"state", state}}}}
- updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
- fmt.Println(updateResult)
- if err != nil {
- fmt.Println(err)
- }
- return updateResult
- }
- func (m *Mgo) GetSortList(filter bson.D, sort bson.D) *mongo.Cursor {
- client := DB.Mongo
- collection, _ := client.Database(m.database).Collection(m.collection).Clone()
- findOptions := options.Find().SetSort(sort)
- singleResult, err := collection.Find(context.TODO(), filter, findOptions)
- if err != nil {
- fmt.Println(err)
- }
- return singleResult
- }
|