/
dbmongo.go
159 lines (119 loc) · 3.38 KB
/
dbmongo.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
package db
import (
"context"
"errors"
"fmt"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
//var collection *mongo.Collection
var ctx = context.TODO()
type MongoDb struct {
mdbInstance *mongo.Database
}
type collectionInfo struct {
entityName string
collectionHdl *mongo.Collection
}
//Connect establishes connection to the database server hosted in "host"
//and returns db handle for the given dbName
func (mdb *MongoDb) Connect(host string, dbName string) DbHandle {
if mdb == nil {
log.Fatal("already connected to a db instance\n")
}
clientOptions := options.Client().ApplyURI("mongodb://" + host + "/")
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
mdb.mdbInstance = client.Database(dbName)
return mdb
}
func (mdb *MongoDb) Create(entityName string) EntityHandle {
return collectionInfo{collectionHdl: mdb.mdbInstance.Collection(entityName), entityName: entityName}
}
func (mdb *MongoDb) GetEntity(entityName string) EntityHandle {
return collectionInfo{collectionHdl: mdb.mdbInstance.Collection(entityName), entityName: entityName}
}
func (mdb *MongoDb) Insert(entity EntityHandle, rowEntry Row) error {
collectionData := entity.(collectionInfo)
collectionHdl := collectionData.collectionHdl
_, err := collectionHdl.InsertOne(ctx, rowEntry)
return err
}
func (mdb *MongoDb) Remove(entity EntityHandle, id ObjectId) error {
collectionData := entity.(collectionInfo)
collectionHdl := collectionData.collectionHdl
filter := bson.D{primitive.E{Key: "_id", Value: id}}
res, err := collectionHdl.DeleteOne(ctx, filter)
if err != nil {
return err
}
if res.DeletedCount == 0 {
return errors.New("no documents were deleted")
}
return nil
}
func (mdb *MongoDb) Get(entity EntityHandle, fn func(func(interface{}) error) (interface{}, error), id ObjectId) (Row, error) {
var row Row
collectionData := entity.(collectionInfo)
collectionHdl := collectionData.collectionHdl
filter := bson.D{primitive.E{Key: "_id", Value: id}}
cur, err := collectionHdl.Find(ctx, filter)
if err != nil {
return nil, err
}
if cur.Next(ctx) {
row, err = fn(cur.Decode)
if err != nil {
return nil, err
}
}
cur.Close(ctx)
return row, nil
}
func filterRows(coll collectionInfo, fn func(func(interface{}) error) (interface{}, error), filter bson.D) ([]Row, error) {
var rows []Row
cur, err := coll.collectionHdl.Find(ctx, filter)
if err != nil {
return nil, err
}
// Iterate through the cursor and decode each document one at a time
for cur.Next(ctx) {
row, err := fn(cur.Decode)
if err != nil {
return nil, err
}
rows = append(rows, row)
}
if err := cur.Err(); err != nil {
return nil, err
}
// once exhausted, close the cursor
cur.Close(ctx)
if len(rows) == 0 {
return nil, mongo.ErrNoDocuments
}
fmt.Println()
return rows, nil
}
func (mdb *MongoDb) GetMatched(entity EntityHandle, fn func(func(interface{}) error) (interface{}, error), match Match) ([]Row, error) {
var (
rows []Row
err error
filter bson.D
)
collectionData := entity.(collectionInfo)
for k, v := range match {
filter = append(filter, primitive.E{Key: k, Value: v})
}
rows, err = filterRows(collectionData, fn, filter)
return rows, err
}