-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb_publishkey.go
46 lines (39 loc) · 1.05 KB
/
db_publishkey.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
package main
import (
"context"
"errors"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func HasPublishKeyV2(client *mongo.Client, id string) bool {
collection := client.Database(*flagDBName).Collection("publishkey")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
n, err := collection.CountDocuments(ctx, bson.M{"id": id})
if err != nil {
return false
}
if n == 0 {
return false
}
return true
}
func addTaskPublishV2(client *mongo.Client, id, task, key string, p Publish) error {
collection := client.Database(*flagDBName).Collection("items")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := HasTaskV2(client, id, task)
if err != nil {
return err
}
result, err := collection.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$push": bson.M{fmt.Sprintf("tasks.%s.publishes.%s", task, key): p}})
if err != nil {
return err
}
if result.MatchedCount == 0 {
return errors.New("no document found with id" + id)
}
return nil
}