-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo5.go
79 lines (68 loc) · 1.99 KB
/
demo5.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"time"
)
/**
* @Author: super
* @Date: 2021-02-02 11:20
* @Description: 删除操作
**/
type TimePoint struct {
StartTime int64 `json:"start_time" bson:"start_time"`
EndTime int64 `json:"end_time" bson:"end_time"`
}
type LogRecord struct {
JobName string `json:"job_name" bson:"job_name"`
Command string `json:"command" bson:"command"`
Err string `json:"err" bson:"err"`
Content string `json:"content" bson:"content"`
TimePoint TimePoint `json:"time_point" bson:"time_point"`
}
// startTime小于某时间
// {"$lt": timestamp}
type TimeBeforeCond struct {
Before int64 `bson:"$lt"`
}
// {"timePoint.startTime": {"$lt": timestamp} }
type DeleteCond struct {
beforeCond TimeBeforeCond `bson:"timePoint.startTime"`
}
func main() {
var (
client *mongo.Client
err error
database *mongo.Database
collection *mongo.Collection
delCond *DeleteCond
delResult *mongo.DeleteResult
)
// 1, 建立连接
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
opt := options.Client().ApplyURI("mongodb://root:password@localhost:27017")
if client, err = mongo.Connect(ctx, opt); err != nil {
panic(err)
} else {
ctx2, _ := context.WithTimeout(context.Background(), 2*time.Second)
err := client.Ping(ctx2, readpref.Primary())
if err != nil {
panic(err)
}
database = client.Database("cron")
}
// 3, 选择表my_collection
collection = database.Collection("log")
// 4, 要删除开始时间早于当前时间的所有日志($lt是less than)
// delete({"timePoint.startTime": {"$lt": 当前时间}})
delCond = &DeleteCond{beforeCond: TimeBeforeCond{Before: time.Now().Unix()}}
// 执行删除
if delResult, err = collection.DeleteMany(context.TODO(), delCond); err != nil {
fmt.Println(err)
return
}
fmt.Println("删除的行数:", delResult.DeletedCount)
}