-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetAuthorList.go
56 lines (51 loc) · 1.03 KB
/
getAuthorList.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
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"regexp"
"strings"
)
func main() {
// Connect to MongoDB
session, err := mgo.Dial("127.0.0.1")
if err != nil {
panic(err)
}
defer session.Close()
// Get the collection
c := session.DB("aozora").C("books_go")
// Use Aggregate to get Author list sorted by book counts
//db.books_go.aggregate([
//{ $group: {
// "_id": "$author",
// "frequency": { "$sum": 1 }
//}},
//{'$match':{"frequency": {'$gt': 50}}},
//{ $sort: { "frequency": 1 } }
//]).count()
m := []bson.M{}
if err = c.Pipe(
[]bson.M{
bson.M{
"$group": bson.M{
"_id": "$author",
"freq": bson.M{"$sum": 1},
},
},
bson.M{
"$match": bson.M{"freq": bson.M{"$gt": 5}},
},
bson.M{
"$sort": bson.M{"freq": -1},
},
},
).All(&m); err == nil { // Do Query
ary := make([]string, len(m))
for _, v := range m {
ary = append(ary, v["_id"].(string))
}
re := regexp.MustCompile(",,+")
fmt.Println(re.ReplaceAllString(strings.Join(ary, ","), ""))
}
}