diff options
Diffstat (limited to 'src/mongo.go')
-rw-r--r-- | src/mongo.go | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/src/mongo.go b/src/mongo.go deleted file mode 100644 index 1d9f74f..0000000 --- a/src/mongo.go +++ /dev/null | |||
@@ -1,98 +0,0 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "context" | ||
5 | "time" | ||
6 | |||
7 | "go.mongodb.org/mongo-driver/bson" | ||
8 | "go.mongodb.org/mongo-driver/mongo" | ||
9 | "go.mongodb.org/mongo-driver/mongo/options" | ||
10 | ) | ||
11 | |||
12 | type mongoClient struct { | ||
13 | dbName, colName string | ||
14 | client *mongo.Client | ||
15 | col *mongo.Collection | ||
16 | } | ||
17 | |||
18 | func (mc *mongoClient) init(dbName, colName string) error { | ||
19 | var err error | ||
20 | if err = mc.Connect(dbName, colName); err != nil { | ||
21 | return err | ||
22 | } | ||
23 | if err = mc.Drop(); err != nil { | ||
24 | return err | ||
25 | } | ||
26 | |||
27 | return nil | ||
28 | } | ||
29 | |||
30 | func (mc *mongoClient) Connect(dbName, colName string) error { | ||
31 | var err error | ||
32 | mc.client, err = mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017")) | ||
33 | |||
34 | if err != nil { | ||
35 | return err | ||
36 | } | ||
37 | |||
38 | ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) | ||
39 | err = mc.client.Connect(ctx) | ||
40 | if err != nil { | ||
41 | return err | ||
42 | } | ||
43 | |||
44 | mc.col = mc.client.Database(dbName).Collection(colName) | ||
45 | mc.dbName = dbName | ||
46 | mc.colName = colName | ||
47 | return nil | ||
48 | } | ||
49 | |||
50 | func (mc *mongoClient) InsertOne(document interface{}) error { | ||
51 | _, err := mc.col.InsertOne(context.Background(), document) | ||
52 | return err | ||
53 | } | ||
54 | |||
55 | func (mc *mongoClient) UpdateOne(filter, update interface{}) error { | ||
56 | _, err := mc.col.UpdateOne(context.Background(), filter, update) | ||
57 | return err | ||
58 | } | ||
59 | |||
60 | func (mc *mongoClient) UpdateMany(filter, update interface{}) error { | ||
61 | _, err := mc.col.UpdateMany(context.Background(), filter, update) | ||
62 | return err | ||
63 | } | ||
64 | |||
65 | func (mc *mongoClient) Finddoc(filter bson.M) ([]bson.M, error) { | ||
66 | cur, err := mc.col.Find(context.Background(), filter) | ||
67 | if err != nil { | ||
68 | return nil, err | ||
69 | } | ||
70 | |||
71 | var results []bson.M | ||
72 | err = cur.All(context.Background(), &results) | ||
73 | |||
74 | return results, err | ||
75 | } | ||
76 | |||
77 | func (mc *mongoClient) FindOneAndDelete(filter bson.M) (bson.M, error) { | ||
78 | res := mc.col.FindOneAndDelete(context.Background(), filter) | ||
79 | var result bson.M | ||
80 | err := res.Decode(&result) | ||
81 | return result, err | ||
82 | } | ||
83 | |||
84 | func (mc *mongoClient) Drop() error { | ||
85 | return mc.col.Drop(context.Background()) | ||
86 | } | ||
87 | |||
88 | func (mc *mongoClient) Disconnect() error { | ||
89 | err := mc.client.Disconnect(context.Background()) | ||
90 | if err != nil { | ||
91 | return err | ||
92 | } | ||
93 | mc.col = nil | ||
94 | mc.client = nil | ||
95 | mc.dbName = "" | ||
96 | mc.colName = "" | ||
97 | return nil | ||
98 | } | ||