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) FindOneByBson(filter interface{}) *mongo.SingleResult { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //collection. 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) FindPipeline(filter bson.A) *mongo.Cursor { client := DB.Mongo collection, _ := client.Database(m.database).Collection(m.collection).Clone() //collection. //filter := bson.D{{key, value}} singleResult, _ := collection.Aggregate(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 }