mgo_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "go.mongodb.org/mongo-driver/bson/primitive"
  7. "go.mongodb.org/mongo-driver/mongo"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. "go.mongodb.org/mongo-driver/mongo/readpref"
  10. "log"
  11. "testing"
  12. "time"
  13. )
  14. func TestMgo1(T *testing.T) {
  15. type Item struct {
  16. ID primitive.ObjectID `bson:"_id,omitempty"`
  17. Site string `bson:"site"`
  18. Channel string `bson:"channel"`
  19. Publishtime string `bson:"publishtime"`
  20. // Add other fields as needed
  21. }
  22. type GroupedData struct {
  23. Channel string `bson:"channel"`
  24. Count int `bson:"count"`
  25. }
  26. // 设置MongoDB连接选项
  27. //clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:27081")
  28. clientOptions := options.Client().ApplyURI("mongodb://172.17.4.87:27080")
  29. //clientOptions := options.Client().ApplyURI("mongodb://SJZY_RWbid_ES:SJZY%40B4i4D5e6S@172.17.145.163:27083")
  30. clientOptions.SetReadPreference(readpref.Primary())
  31. clientOptions.SetDirect(true)
  32. // 连接MongoDB
  33. client, err := mongo.Connect(context.Background(), clientOptions)
  34. if err != nil {
  35. log.Println(err)
  36. }
  37. // 检查连接
  38. err = client.Ping(context.Background(), nil)
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. // 选择数据库和集合
  43. database := client.Database("qlm")
  44. collection := database.Collection("data_merge")
  45. // 查询条件
  46. filter := bson.D{
  47. {"site", "千里马"},
  48. {"publishtime", bson.D{{"$gt", "2022-01-01"}}},
  49. }
  50. // 执行查询
  51. cursor, err := collection.Find(context.Background(), filter)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. defer cursor.Close(context.Background())
  56. // 手动分组查看数据
  57. groupedData := make(map[string]int)
  58. for cursor.Next(context.Background()) {
  59. var item Item
  60. err := cursor.Decode(&item)
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. // 在这里可以根据需要进行其他逻辑处理
  65. // 手动分组
  66. groupedData[item.Channel]++
  67. }
  68. // 输出结果
  69. //for channel, count := range groupedData {
  70. // fmt.Printf("Channel: %s, Count: %d\n", channel, count)
  71. //}
  72. // 将手动分组的数据保存到新的集合中
  73. var groupedDataList []interface{}
  74. for channel, count := range groupedData {
  75. groupedDataList = append(groupedDataList, GroupedData{Channel: channel, Count: count})
  76. }
  77. // 选择新的集合
  78. newCollection := database.Collection("wcc_qlm")
  79. // 插入数据
  80. _, err = newCollection.InsertMany(context.Background(), groupedDataList)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. fmt.Println("Data inserted into wcc_qlm collection.")
  85. }
  86. func Test2(T *testing.T) {
  87. // Assume you have a MongoDB _id
  88. idString := "5f289ea352c1d9fbf84e1f1b"
  89. // Convert the string to ObjectId
  90. objectID, err := primitive.ObjectIDFromHex(idString)
  91. if err != nil {
  92. fmt.Println("Error parsing ObjectId:", err)
  93. return
  94. }
  95. // Extract the time portion from ObjectId
  96. timestamp := objectID.Timestamp()
  97. //timeFromTimestamp := time.Unix(int64(timestamp.Unix()), 0)
  98. // Print timestamp and corresponding time
  99. fmt.Printf("Timestamp: %v\n", timestamp)
  100. fmt.Printf("Time from Timestamp: %v\n", timestamp.Unix())
  101. oneYearAgo := time.Now().AddDate(-1, 0, 0)
  102. isWithinOneYear := timestamp.After(oneYearAgo)
  103. if isWithinOneYear {
  104. fmt.Println(111)
  105. } else {
  106. fmt.Println(222)
  107. }
  108. }