mgo.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/util/gconv"
  6. "gmanager/library/mongo/util"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. "strconv"
  12. "time"
  13. )
  14. type Mgo struct {
  15. database string
  16. collection string
  17. }
  18. func NewMgo(database, collection string) *Mgo {
  19. return &Mgo{
  20. database,
  21. collection,
  22. }
  23. }
  24. // 查询单个
  25. func (m *Mgo) FindOne(key string, value interface{}) *mongo.SingleResult {
  26. client := DB.Mongo
  27. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  28. //collection.
  29. filter := bson.D{{key, value}}
  30. singleResult := collection.FindOne(context.TODO(), filter)
  31. return singleResult
  32. }
  33. // 查询单个
  34. func (m *Mgo) FindOne_ServerId(key string, value interface{}, serverId string) *mongo.SingleResult {
  35. client := DB.Mongo
  36. collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
  37. //collection.
  38. filter := bson.D{{key, value}}
  39. singleResult := collection.FindOne(context.TODO(), filter)
  40. return singleResult
  41. }
  42. func (m *Mgo) FindOneBson(filter bson.D) int64 {
  43. client := DB.Mongo
  44. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  45. //collection.
  46. //filter := bson.D{{key, value}}
  47. singleResult, _ := collection.CountDocuments(context.TODO(), filter)
  48. return singleResult
  49. }
  50. func (m *Mgo) FindOneBson_ServerId(filter bson.D, serverId string) int64 {
  51. client := DB.Mongo
  52. collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
  53. singleResult, _ := collection.CountDocuments(context.TODO(), filter)
  54. return singleResult
  55. }
  56. //// 查询单个
  57. //func (m *Mgo) FindExit(key string, value interface{}) *mongo.SingleResult {
  58. // client := DB.Mongo
  59. // collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  60. // //collection.
  61. // filter := bson.D{{key, value}}
  62. // singleResult := collection.fub(context.TODO(), filter)
  63. // return singleResult
  64. //}
  65. // 查询多个
  66. func (m *Mgo) FindAll(key string, value interface{}) *mongo.Cursor {
  67. client := DB.Mongo
  68. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  69. //collection.
  70. filter := bson.D{{key, value}}
  71. singleResult, _ := collection.Find(context.TODO(), filter)
  72. return singleResult
  73. }
  74. func (m *Mgo) FindAllBson(filter bson.D) *mongo.Cursor {
  75. client := DB.Mongo
  76. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  77. //collection.
  78. //filter := bson.D{{key, value}}
  79. singleResult, _ := collection.Find(context.TODO(), filter)
  80. return singleResult
  81. }
  82. func (m *Mgo) FindAllBson_ServerId(filter bson.D, serverId string) *mongo.Cursor {
  83. client := DB.Mongo
  84. collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
  85. singleResult, _ := collection.Find(context.TODO(), filter)
  86. return singleResult
  87. }
  88. //插入单个
  89. func (m *Mgo) InsertOne(value interface{}) *mongo.InsertOneResult {
  90. client := DB.Mongo
  91. collection := client.Database(m.database).Collection(m.collection)
  92. insertResult, err := collection.InsertOne(context.TODO(), value)
  93. if err != nil {
  94. fmt.Println(err)
  95. }
  96. return insertResult
  97. }
  98. //插入单个
  99. func (m *Mgo) InsertOne_ServerId(value interface{}, serverId string) *mongo.InsertOneResult {
  100. client := DB.Mongo
  101. collection := client.Database(m.database).Collection(m.collection + "_" + serverId)
  102. insertResult, err := collection.InsertOne(context.TODO(), value)
  103. if err != nil {
  104. fmt.Println(err)
  105. }
  106. return insertResult
  107. }
  108. func (m *Mgo) InsertOneByTime(value interface{}, logtime string) *mongo.InsertOneResult {
  109. client := DB.Mongo
  110. collection := client.Database(m.database).Collection(m.collection + "_" + logtime)
  111. insertResult, err := collection.InsertOne(context.TODO(), value)
  112. if err != nil {
  113. fmt.Println(err)
  114. }
  115. return insertResult
  116. }
  117. //更新单个
  118. func (m *Mgo) UpdateOne(key primitive.ObjectID, value interface{}) int64 {
  119. client := DB.Mongo
  120. collection := client.Database(m.database).Collection(m.collection)
  121. filter := bson.D{{"_id", key}}
  122. update := bson.D{{"$set", value}}
  123. count, err := collection.UpdateOne(context.TODO(), filter, update)
  124. if err != nil {
  125. fmt.Println(err)
  126. }
  127. return count.UpsertedCount
  128. }
  129. func (m *Mgo) UpdateOneValue(key string, num int, itemyype string, serverid int) int64 {
  130. client := DB.Mongo
  131. collection := client.Database(m.database).Collection(m.collection)
  132. updateOpts := options.Update().SetUpsert(true)
  133. filter := bson.D{{"ItemName", key}, {"serverid", serverid}}
  134. update := bson.D{{"$set", bson.D{{"ItemName", key}, {"type", itemyype}}},
  135. {"$inc", bson.D{{"itemNum", num}}}}
  136. count, err := collection.UpdateOne(context.TODO(), filter, update, updateOpts)
  137. if err != nil {
  138. fmt.Println(err)
  139. }
  140. filter = bson.D{{"ItemName", key}, {"serverid", 0}}
  141. count, err = collection.UpdateOne(context.TODO(), filter, update, updateOpts)
  142. return count.UpsertedCount
  143. }
  144. func (m *Mgo) UpdateOneValueByTime(key string, num int, logtime string, itemyype string, serverid int) int64 {
  145. client := DB.Mongo
  146. collection := client.Database(m.database).Collection(m.collection + "_" + logtime)
  147. updateOpts := options.Update().SetUpsert(true)
  148. filter := bson.D{{"ItemName", key}, {"serverid", serverid}}
  149. update := bson.D{{"$set", bson.D{{"ItemName", key}, {"type", itemyype}}},
  150. {"$inc", bson.D{{"itemNum", num}}}}
  151. count, err := collection.UpdateOne(context.TODO(), filter, update, updateOpts)
  152. if err != nil {
  153. fmt.Println(err)
  154. }
  155. filter = bson.D{{"ItemName", key}, {"serverid", 0}}
  156. count, err = collection.UpdateOne(context.TODO(), filter, update, updateOpts)
  157. return count.UpsertedCount
  158. }
  159. //查询集合里有多少数据
  160. func (m *Mgo) CollectionCount() (string, int64) {
  161. client := DB.Mongo
  162. collection := client.Database(m.database).Collection(m.collection)
  163. name := collection.Name()
  164. size, err := collection.EstimatedDocumentCount(context.TODO())
  165. if err != nil {
  166. fmt.Println(err)
  167. }
  168. return name, size
  169. }
  170. //按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取
  171. func (m *Mgo) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor {
  172. client := DB.Mongo
  173. collection := client.Database(m.database).Collection(m.collection)
  174. SORT := bson.D{{"_id", sort}} //filter := bson.D{{key,value}}
  175. filter := bson.D{{}}
  176. findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip)
  177. temp, _ := collection.Find(context.Background(), filter, findOptions)
  178. return temp
  179. }
  180. //获取集合创建时间和编号
  181. func (m *Mgo) ParsingId(result string) (time.Time, uint64) {
  182. temp1 := result[:8]
  183. timestamp, _ := strconv.ParseInt(temp1, 16, 64)
  184. dateTime := time.Unix(timestamp, 0) //这是截获情报时间 时间格式 2019-04-24 09:23:39 +0800 CST
  185. temp2 := result[18:]
  186. count, _ := strconv.ParseUint(temp2, 16, 64) //截获情报的编号
  187. return dateTime, count
  188. }
  189. //删除文章和查询文章
  190. func (m *Mgo) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) {
  191. client := DB.Mongo
  192. collection := client.Database(m.database).Collection(m.collection)
  193. filter := bson.D{{key, value}}
  194. singleResult := collection.FindOne(context.TODO(), filter)
  195. DeleteResult, err := collection.DeleteOne(context.TODO(), filter, nil)
  196. if err != nil {
  197. fmt.Println("删除时出现错误,你删不掉的~")
  198. }
  199. return DeleteResult.DeletedCount, singleResult
  200. }
  201. //删除文章
  202. func (m *Mgo) Delete(key string, value interface{}) int64 {
  203. client := DB.Mongo
  204. collection := client.Database(m.database).Collection(m.collection)
  205. filter := bson.D{{key, value}}
  206. count, err := collection.DeleteOne(context.TODO(), filter, nil)
  207. if err != nil {
  208. fmt.Println(err)
  209. }
  210. return count.DeletedCount
  211. }
  212. //删除多个
  213. func (m *Mgo) DeleteMany(key string, value interface{}) int64 {
  214. client := DB.Mongo
  215. collection := client.Database(m.database).Collection(m.collection)
  216. filter := bson.D{{key, value}}
  217. count, err := collection.DeleteMany(context.TODO(), filter)
  218. if err != nil {
  219. fmt.Println(err)
  220. }
  221. return count.DeletedCount
  222. }
  223. func (m *Mgo) GetPageList(filter bson.D, page util.Page) *mongo.Cursor {
  224. client := DB.Mongo
  225. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  226. SORT := bson.D{{"_id", -1}} //1:升序; -1:降序
  227. findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1)))
  228. singleResult, err := collection.Find(context.TODO(), filter, findOptions)
  229. if err != nil {
  230. fmt.Println(err)
  231. }
  232. return singleResult
  233. }
  234. func (m *Mgo) GetPageList_ServerId(filter bson.D, page util.Page, serverId string) *mongo.Cursor {
  235. client := DB.Mongo
  236. collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
  237. SORT := bson.D{{"_id", -1}} //1:升序; -1:降序
  238. findOptions := options.Find().SetSort(SORT).SetLimit(page.PageSize).SetSkip(gconv.Int64(page.PageSize * (page.CurrentPage - 1)))
  239. singleResult, err := collection.Find(context.TODO(), filter, findOptions)
  240. if err != nil {
  241. fmt.Println(err)
  242. }
  243. return singleResult
  244. }
  245. //查询数量
  246. func (m *Mgo) GetListCount(filter bson.D) int {
  247. client := DB.Mongo
  248. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  249. singleResult, err := collection.Find(context.TODO(), filter)
  250. if err != nil {
  251. fmt.Println(err)
  252. }
  253. var showsWithInfo []bson.M
  254. if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil {
  255. fmt.Println(err)
  256. }
  257. return len(showsWithInfo)
  258. }
  259. //查询数量
  260. func (m *Mgo) GetListCountByServer(filter bson.D, serverId string) int {
  261. client := DB.Mongo
  262. collection, _ := client.Database(m.database).Collection(m.collection + "_" + serverId).Clone()
  263. singleResult, err := collection.Find(context.TODO(), filter)
  264. if err != nil {
  265. fmt.Println(err)
  266. }
  267. var showsWithInfo []bson.M
  268. if err = singleResult.All(context.TODO(), &showsWithInfo); err != nil {
  269. fmt.Println(err)
  270. }
  271. return len(showsWithInfo)
  272. }
  273. //查询数量
  274. func (m *Mgo) GetListCountByServer2(filter bson.D, serverId string) int {
  275. client := DB.Mongo
  276. collection, _ := client.Database(m.database + serverId).Collection(m.collection).Clone()
  277. count, err := collection.CountDocuments(context.TODO(), filter)
  278. if err != nil {
  279. fmt.Println(err)
  280. }
  281. return int(count)
  282. }
  283. func (m *Mgo) InsUpdate(id string, state int) *mongo.UpdateResult {
  284. obj_id, _ := primitive.ObjectIDFromHex(id)
  285. client := DB.Mongo
  286. collection := client.Database(m.database).Collection(m.collection)
  287. filter := bson.D{{"_id", obj_id}}
  288. update := bson.D{{"$set", bson.D{{"state", state}}}}
  289. updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
  290. fmt.Println(updateResult)
  291. if err != nil {
  292. fmt.Println(err)
  293. }
  294. return updateResult
  295. }
  296. // 参数类型不同
  297. func (m *Mgo) InsUpdate2(id int32, state string) *mongo.UpdateResult {
  298. client := DB.Mongo
  299. collection := client.Database(m.database).Collection(m.collection)
  300. filter := bson.D{{"_id", id}}
  301. update := bson.D{{"$set", bson.D{{"state", state}}}}
  302. updateResult, err := collection.UpdateOne(context.TODO(), filter, update)
  303. fmt.Println(updateResult)
  304. if err != nil {
  305. fmt.Println(err)
  306. }
  307. return updateResult
  308. }
  309. func (m *Mgo) GetSortList(filter bson.D, sort bson.D) *mongo.Cursor {
  310. client := DB.Mongo
  311. collection, _ := client.Database(m.database).Collection(m.collection).Clone()
  312. findOptions := options.Find().SetSort(sort)
  313. singleResult, err := collection.Find(context.TODO(), filter, findOptions)
  314. if err != nil {
  315. fmt.Println(err)
  316. }
  317. return singleResult
  318. }