mgo.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package mgodb
  2. import (
  3. "context"
  4. "fmt"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. "go.mongodb.org/mongo-driver/mongo/options"
  8. "strconv"
  9. "time"
  10. )
  11. type mgo struct {
  12. database string
  13. collection string
  14. client *mongo.Client
  15. }
  16. func NewMgo(database, collection string, client *mongo.Client) *mgo {
  17. return &mgo{
  18. database,
  19. collection,
  20. client,
  21. }
  22. }
  23. // 查询单个
  24. func (m *mgo) FindOne(key string, value interface{}) *mongo.SingleResult {
  25. collection, _ := m.client.Database(m.database).Collection(m.collection).Clone()
  26. filter := bson.D{{key, value}}
  27. singleResult := collection.FindOne(context.TODO(), filter)
  28. return singleResult
  29. }
  30. // 查询单个
  31. func (m *mgo) Find(filter bson.D, findOptions *options.FindOptions) (cur *mongo.Cursor, err error) {
  32. collection := m.client.Database(m.database).Collection(m.collection)
  33. cur, err = collection.Find(context.TODO(), filter, findOptions)
  34. if err != nil {
  35. return
  36. }
  37. return
  38. }
  39. // 插入单个
  40. func (m *mgo) InsertOne(value interface{}) (insertResult *mongo.InsertOneResult, err error) {
  41. collection := m.client.Database(m.database).Collection(m.collection)
  42. insertResult, err = collection.InsertOne(context.TODO(), value)
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. return
  48. }
  49. func (m *mgo) InsertMany(value []interface{}) (insertManyResult *mongo.InsertManyResult, err error) {
  50. collection := m.client.Database(m.database).Collection(m.collection)
  51. insertManyResult, err = collection.InsertMany(context.TODO(), value)
  52. if err != nil {
  53. return
  54. }
  55. fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs)
  56. return
  57. }
  58. // 查询集合里有多少数据
  59. func (m *mgo) CollectionCount() (string, int64) {
  60. collection := m.client.Database(m.database).Collection(m.collection)
  61. name := collection.Name()
  62. size, _ := collection.EstimatedDocumentCount(context.TODO())
  63. return name, size
  64. }
  65. // 按选项查询集合 Skip 跳过 Limit 读取数量 sort 1 ,-1 . 1 为最初时间读取 , -1 为最新时间读取
  66. func (m *mgo) CollectionDocuments(Skip, Limit int64, sort int) *mongo.Cursor {
  67. collection := m.client.Database(m.database).Collection(m.collection)
  68. SORT := bson.D{{"_id", sort}} //filter := bson.D{{key,value}}
  69. filter := bson.D{{}}
  70. findOptions := options.Find().SetSort(SORT).SetLimit(Limit).SetSkip(Skip)
  71. //findOptions.SetLimit(i)
  72. temp, _ := collection.Find(context.Background(), filter, findOptions)
  73. return temp
  74. }
  75. // 获取集合创建时间和编号
  76. func (m *mgo) ParsingId(result string) (time.Time, uint64) {
  77. temp1 := result[:8]
  78. timestamp, _ := strconv.ParseInt(temp1, 16, 64)
  79. dateTime := time.Unix(timestamp, 0) //这是截获情报时间 时间格式 2019-04-24 09:23:39 +0800 CST
  80. temp2 := result[18:]
  81. count, _ := strconv.ParseUint(temp2, 16, 64) //截获情报的编号
  82. return dateTime, count
  83. }
  84. // 删除文章和查询文章
  85. func (m *mgo) DeleteAndFind(key string, value interface{}) (int64, *mongo.SingleResult) {
  86. collection := m.client.Database(m.database).Collection(m.collection)
  87. filter := bson.D{{key, value}}
  88. singleResult := collection.FindOne(context.TODO(), filter)
  89. DeleteResult, err := collection.DeleteOne(context.TODO(), filter, nil)
  90. if err != nil {
  91. fmt.Println("删除时出现错误,你删不掉的~")
  92. }
  93. return DeleteResult.DeletedCount, singleResult
  94. }
  95. // 删除文章
  96. func (m *mgo) Delete(key string, value interface{}) int64 {
  97. collection := m.client.Database(m.database).Collection(m.collection)
  98. filter := bson.D{{key, value}}
  99. count, err := collection.DeleteOne(context.TODO(), filter, nil)
  100. if err != nil {
  101. fmt.Println(err)
  102. }
  103. return count.DeletedCount
  104. }
  105. // 删除多个
  106. func (m *mgo) DeleteMany(filter bson.D) (num int64, err error) {
  107. collection := m.client.Database(m.database).Collection(m.collection)
  108. //filter := bson.D{{key, value}}
  109. count, err := collection.DeleteMany(context.TODO(), filter)
  110. if err != nil {
  111. return
  112. }
  113. num = count.DeletedCount
  114. return
  115. }
  116. func (m *mgo) UpdateMany(filter, update bson.D) (result *mongo.UpdateResult, err error) {
  117. collection := m.client.Database(m.database).Collection(m.collection)
  118. //filter := bson.D{{"name", "123456"}}
  119. //update := bson.D{{"$set", bson.D{{"name", "张三"}}}}
  120. result, err = collection.UpdateMany(context.TODO(), filter, update)
  121. if err != nil {
  122. return
  123. }
  124. fmt.Println(result)
  125. return
  126. }
  127. func (m *mgo) UpdateOne(filter, update bson.D) (result *mongo.UpdateResult, err error) {
  128. collection := m.client.Database(m.database).Collection(m.collection)
  129. //filter := bson.D{{"name", "123456"}}
  130. //update := bson.D{{"$set", bson.D{{"name", "张三"}}}}
  131. result, err = collection.UpdateOne(context.TODO(), filter, update)
  132. if err != nil {
  133. return
  134. }
  135. fmt.Println(result)
  136. return
  137. }