-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
66 lines (50 loc) · 1.61 KB
/
find.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
package mongohelper
import (
"context"
"errors"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
//func cursorClose(rs *mongo.Cursor) {
// if err := rs.Close(context.TODO()); err != nil {
// log.Println(err)
// }
//}
// Find cs wraps the mongo.Database.Collection.Find() method
// It returns a Cursor over the matching documents in the collection.
//
// The filter parameter must be a document containing query operators and can be used to select which documents are
// included in the result. An empty document (e.g. bson.D{}) should be used to include all documents.
func (l *Link) Find(database, collection string, filter interface{}, dest interface{}) error {
if err := l.linkCheck("link.Find"); err != nil {
return err
}
if dest == nil {
return fmt.Errorf(`given "dest" is null`)
}
if filter == nil {
filter = bson.M{}
}
ctx, cancel := context.WithTimeout(context.Background(), l.execTimeout())
defer cancel()
rs, err := l.client.Database(database).Collection(collection).Find(ctx, filter, options.Find())
if err != nil {
// If not connected, try once again, reconnecting. otherwise, just return/leave
if !errors.Is(err, mongo.ErrClientDisconnected) {
return err
}
if err := l.connect(); err != nil {
return err
}
ctx2, cancel2 := context.WithTimeout(context.Background(), l.execTimeout())
defer cancel2()
rs, err = l.client.Database(database).Collection(collection).Find(ctx2, filter, options.Find())
if err != nil {
return err
}
}
//defer cursorClose(rs)
return rs.All(context.TODO(), dest)
}