-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo.go
More file actions
executable file
·117 lines (91 loc) · 2.61 KB
/
Copy pathmongo.go
File metadata and controls
executable file
·117 lines (91 loc) · 2.61 KB
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
package db
import (
"context"
"blvchain/core/config"
"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"
)
func ConnectToMongoDB() (*mongo.Client, error) {
clientOptions := options.Client().ApplyURI(config.MONGO_URI)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
return nil, err
}
err = client.Ping(context.TODO(), nil)
if err != nil {
return nil, err
}
return client, nil
}
func InsertOneBlock(document interface{}) (bool, error) {
_, err := config.BLOCK_COLL.InsertOne(context.TODO(), document)
if err != nil {
return false, err
}
return true, nil
}
func InsertManyBlock(document []interface{}) (bool, error) {
options := options.InsertMany().SetOrdered(false)
_, err := config.BLOCK_COLL.InsertMany(context.TODO(), document, options)
if err != nil {
return false, err
}
return true, nil
}
func FindOneBlock(blockHash string, result interface{}) error {
err := config.BLOCK_COLL.FindOne(context.TODO(), bson.M{"_id": blockHash}).Decode(result)
if err != nil {
return err
}
return nil
}
func FindManyBlocksLimited(filter primitive.M, skip int64, limit int64) ([]Block, error) {
var result []Block
findOptions := options.Find().SetSort(config.DESC).SetLimit(limit).SetSkip(skip)
cursor, find_err := config.BLOCK_COLL.Find(context.TODO(), filter, findOptions)
if find_err != nil {
return result, find_err
}
cursor_err := cursor.All(context.TODO(), &result)
if cursor_err != nil {
return result, cursor_err
}
if result == nil {
return result, mongo.ErrNoDocuments
}
return result, nil
}
func FindManyBlocksLimitedASE(filter primitive.M, skip int64, limit int64) ([]Block, error) {
var result []Block
findOptions := options.Find().SetSort(config.ASC).SetLimit(limit).SetSkip(skip)
cursor, find_err := config.BLOCK_COLL.Find(context.TODO(), filter, findOptions)
if find_err != nil {
return result, find_err
}
cursor_err := cursor.All(context.TODO(), &result)
if cursor_err != nil {
return result, cursor_err
}
if result == nil {
return result, mongo.ErrNoDocuments
}
return result, nil
}
func FindAllBlocks(filter primitive.M) ([]Block, error) {
var result []Block
findOptions := options.Find().SetSort(config.DESC)
cursor, find_err := config.BLOCK_COLL.Find(context.TODO(), filter, findOptions)
if find_err != nil {
return result, find_err
}
cursor_err := cursor.All(context.TODO(), &result)
if cursor_err != nil {
return result, cursor_err
}
if result == nil {
return result, mongo.ErrNoDocuments
}
return result, nil
}