/
atmongo.go
124 lines (108 loc) · 3.51 KB
/
atmongo.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package atdb
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func MongoConnect(mconn DBInfo) (db *mongo.Database) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mconn.DBString))
if err != nil {
fmt.Printf("AIteung Mongo, MongoConnect: %v\n", err)
}
return client.Database(mconn.DBName)
}
func InsertOneDoc(db *mongo.Database, collection string, doc interface{}) (insertedID interface{}) {
insertResult, err := db.Collection(collection).InsertOne(context.TODO(), doc)
if err != nil {
fmt.Printf("AIteung Mongo, InsertOneDoc: %v\n", err)
}
return insertResult.InsertedID
}
func GetOneDoc[T any](db *mongo.Database, collection string, filter bson.M) (doc T) {
err := db.Collection(collection).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("GetOneDoc: %v\n", err)
}
return
}
func GetOneLatestDoc[T any](db *mongo.Database, collection string, filter bson.M) (doc T, err error) {
opts := options.FindOne().SetSort(bson.M{"$natural": -1})
err = db.Collection(collection).FindOne(context.TODO(), filter, opts).Decode(&doc)
if err != nil {
return
}
return
}
func GetAllDocByFilter[T any](db *mongo.Database, collection string, filter bson.M) (doc T) {
ctx := context.TODO()
cur, err := db.Collection(collection).Find(ctx, filter)
if err != nil {
fmt.Printf("GetAllDoc: %v\n", err)
}
defer cur.Close(ctx)
err = cur.All(ctx, &doc)
if err != nil {
fmt.Printf("GetAllDoc Cursor Err: %v\n", err)
}
return
}
func GetAllDoc[T any](db *mongo.Database, collection string) (doc T) {
ctx := context.TODO()
cur, err := db.Collection(collection).Find(ctx, bson.M{})
if err != nil {
fmt.Printf("GetAllDoc: %v\n", err)
}
defer cur.Close(ctx)
err = cur.All(ctx, &doc)
if err != nil {
fmt.Printf("GetAllDoc Cursor Err: %v\n", err)
}
return
}
func GetAllDistinctDoc(db *mongo.Database, filter bson.M, fieldname, collection string) (doc []any) {
ctx := context.TODO()
doc, err := db.Collection(collection).Distinct(ctx, fieldname, filter)
if err != nil {
fmt.Printf("GetAllDistinctDoc: %v\n", err)
}
return
}
func UpdateDoc(db *mongo.Database, collection string, filter bson.M, updatefield bson.M) (updateresult *mongo.UpdateResult, err error) {
updateresult, err = db.Collection(collection).UpdateOne(context.TODO(), filter, updatefield)
return
}
func ReplaceOneDoc(db *mongo.Database, collection string, filter bson.M, doc interface{}) (updatereseult *mongo.UpdateResult) {
updatereseult, err := db.Collection(collection).ReplaceOne(context.TODO(), filter, doc)
if err != nil {
fmt.Printf("ReplaceOneDoc: %v\n", err)
}
return
}
func DeleteOneDoc(db *mongo.Database, collection string, filter bson.M) (result *mongo.DeleteResult) {
result, err := db.Collection(collection).DeleteOne(context.TODO(), filter)
if err != nil {
fmt.Printf("DeleteOneDoc: %v\n", err)
}
return
}
func DeleteDoc(db *mongo.Database, collection string, filter bson.M) (result *mongo.DeleteResult) {
result, err := db.Collection(collection).DeleteMany(context.TODO(), filter)
if err != nil {
fmt.Printf("DeleteDoc : %v\n", err)
}
return
}
func GetRandomDoc[T any](db *mongo.Database, collection string, size uint) (result []T, err error) {
filter := mongo.Pipeline{
{{Key: "$sample", Value: bson.D{{Key: "size", Value: size}}}},
}
ctx := context.Background()
cursor, err := db.Collection(collection).Aggregate(ctx, filter)
if err != nil {
return
}
err = cursor.All(ctx, &result)
return
}