-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_directory.go
126 lines (112 loc) · 3.15 KB
/
query_directory.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
package gohoa
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DirQueryService struct {
DBService
}
func NewDirQueryService() *DirQueryService {
dbSvc := createDBService("directory")
return &DirQueryService{dbSvc}
}
func (s *DirQueryService) FindAllMembers(members *[]Member) error {
ctx := context.TODO()
cursor, err := s.collection.Find(ctx, bson.D{})
if err != nil {
log.Println("Error finding all", err)
return err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var member Member
err := cursor.Decode(&member)
if err != nil {
log.Println("Error decoding member: ", err)
return err
}
*members = append(*members, member)
}
return nil
}
func (s *DirQueryService) FindMembersByStreetNumber(number string) ([]PropertyAddress, error) {
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
ctx := context.TODO()
regExStr := fmt.Sprintf("^[A-Z]{3}-%s", number)
// regExFilter := bson.E{Key: "_id", Value: bson.D{{"$regex", primitive.Regex{Pattern: regExtStr, Options: ""}}}}
// bson.E{Key:"bar", Value: bson.D{{"$regex", primitive.Regex{Pattern:"^ThisValue.*", Options:"i"}},
abandonFilter := bson.D{{"abandoned", bson.D{{"$exists", 0}}}}
regExFilter := bson.D{{"_id", bson.D{{"$regex", regExStr}}}}
logicalAndFilter := bson.D{
{"$and",
bson.A{
regExFilter,
abandonFilter,
},
},
}
projection := bson.D{{Key: "paddress", Value: 1}}
var addresses []PropertyAddress
cursor, err := s.collection.Find(ctx, logicalAndFilter, options.Find().SetProjection(projection))
if err != nil {
log.Println("Error finding members by street number: ", err)
return addresses, err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var member Member
err := cursor.Decode(&member)
if err != nil {
log.Println("Error decoding member: ", err)
return addresses, err
}
addresses = append(addresses, member.PAddress)
}
return addresses, nil
}
func (s *DirQueryService) FindMemberById(memberId int) (Member, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.D{primitive.E{Key: "memberid", Value: memberId}}
var member Member
err := s.collection.FindOne(ctx, filter).Decode(&member)
if err != nil {
log.Println("Error finding member by id: ", err)
return member, err
}
return member, nil
}
func (s *DirQueryService) FindCountByStreetName() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
pipeline := bson.A{
bson.D{
{"$group",
bson.D{
{"_id", "$paddress.streetname"},
{"total", bson.D{{"$sum", 1}}},
},
},
},
bson.D{{"$sort", bson.D{{"total", -1}}}},
}
cursor, err := s.collection.Aggregate(ctx, pipeline)
if err != nil {
log.Println("Error aggregating by street name: ", err)
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var result bson.M
err := cursor.Decode(&result)
if err != nil {
log.Println("Error decoding aggregate result: ", err)
}
fmt.Printf("Street: %s, total: %d\n", result["_id"], result["total"])
}
}