/
mongo.go
37 lines (28 loc) · 869 Bytes
/
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
package database
import (
"context"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func connect() *mongo.Database {
// err := mgm.SetDefaultConfig(nil, "primary", options.Client().ApplyURI("mongodb+srv://anc:courses@primary.hsesw.mongodb.net/primarydb?retryWrites=true&w=majority"))
// if err != nil {
print(12)
// }
clientOptions := options.Client().ApplyURI("mongodb+srv://anc:courses@primary.hsesw.mongodb.net/primarydb?retryWrites=true&w=majority")
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
print(err.Error())
}
err = client.Ping(context.TODO(), nil)
if err != nil {
print(err.Error())
}
log.Printf("Connected to MongoDB!")
database := client.Database("primarydb")
return database
}
// AdminCollection sadsd
var MongoClient = connect()