/
mongo.go
240 lines (215 loc) · 6.5 KB
/
mongo.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package befous
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) {
clientOptions := options.Client().ApplyURI((mconn.DBString))
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
fmt.Printf("Error connecting to MongoDB: %v", err)
}
return client.Database(mconn.DBName)
}
func Create2dsphere(mconn DBInfo) (db *mongo.Database) {
clientOptions := options.Client().ApplyURI((mconn.DBString))
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
fmt.Printf("Error connecting to MongoDB: %v", err)
}
indexModel := mongo.IndexModel{
Keys: bson.D{
{Key: "geometry", Value: "2dsphere"},
},
}
_, err = client.Database(mconn.DBName).Collection(mconn.CollectionName).Indexes().CreateOne(context.TODO(), indexModel)
if err != nil {
fmt.Printf("Error creating geospatial index: %v", 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 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
}
func DocExists[T any](db *mongo.Database, collname string, filter bson.M, doc T) (result bool) {
err := db.Collection(collname).FindOne(context.Background(), filter).Decode(&doc)
return err == nil
}
func GetGeoIntersectsDoc(db *mongo.Database, collname string, coordinates Point) (result string) {
filter := bson.M{
"geometry": bson.M{
"$geoIntersects": bson.M{
"$geometry": bson.M{
"type": "Point",
"coordinates": coordinates.Coordinates,
},
},
},
}
var doc FullGeoJson
err := db.Collection(collname).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("GeoIntersects: %v\n", err)
}
return "Koordinat anda bersinggungan dengan " + doc.Properties.Name
}
func GetGeoWithinDoc(db *mongo.Database, collname string, coordinates Polygon) (result string) {
filter := bson.M{
"geometry": bson.M{
"$geoWithin": bson.M{
"$geometry": bson.M{
"type": "Polygon",
"coordinates": coordinates.Coordinates,
},
},
},
}
var doc FullGeoJson
err := db.Collection(collname).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("GeoWithin: %v\n", err)
}
return "Koordinat anda berada di " + doc.Properties.Name
}
func GetNearDoc(db *mongo.Database, collname string, coordinates Point) (result string) {
filter := bson.M{
"geometry": bson.M{
"$near": bson.M{
"$geometry": bson.M{
"type": "Point",
"coordinates": coordinates.Coordinates,
},
"$maxDistance": 1000,
},
},
}
var doc FullGeoJson
err := db.Collection(collname).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("Near: %v\n", err)
}
return "Koordinat anda dekat dengan " + doc.Properties.Name
}
func GetNearSphereDoc(db *mongo.Database, collname string, coordinates Point) (result string) {
filter := bson.M{
"geometry": bson.M{
"$nearSphere": bson.M{
"$geometry": bson.M{
"type": "Point",
"coordinates": coordinates.Coordinates,
},
"$maxDistance": 1000,
},
},
}
var doc FullGeoJson
err := db.Collection(collname).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("NearSphere: %v\n", err)
}
return "Koordinat anda dekat dengan " + doc.Properties.Name
}
func GetBoxDoc(db *mongo.Database, collname string, coordinates Polyline) (result string) {
filter := bson.M{
"geometry": bson.M{
"$geoWithin": bson.M{
"$box": coordinates.Coordinates,
},
},
}
var doc FullGeoJson
err := db.Collection(collname).FindOne(context.TODO(), filter).Decode(&doc)
if err != nil {
fmt.Printf("Box: %v\n", err)
}
return "Box anda berada pada " + doc.Properties.Name
}