-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
88 lines (74 loc) · 1.65 KB
/
main.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
package main
import (
"dblogin"
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"html/template"
"net/http"
"time"
)
var tmpl = `<html>
<head>
<title>List</title>
</head>
<body>
<p>
<a href="/">main</a> |
<a href="/view/">view</a>
</p>
<h1>Members</h1>
<ul>
{{range .List}}
<li>{{.Name}} - {{.Phone}} - {{.ID}} - {{.Timestamp}}</li>
{{end}}
</ul>
</body>
</html>
`
//{{.Timestamp}}
type personaFields struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string
Phone string
Timestamp time.Time
}
var results []personaFields
func main() {
session, err := mgo.Dial(dblogin.Userpass) // mongodb://username:yourpasscode@serverip:27017/database?authSource=admin
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
// Collection People
c := session.DB("test").C("people")
// Query All using pipeline
pipe := c.Pipe([]bson.M{
{"$project": bson.M{
"name": true, // 1 or true will work, but false is not working
"_id": 1,
"phone": "0", // zero must be in quotations
"timestamp": 1,
}},
{"$match": bson.M{"name": bson.M{"$ne": "Alex1"}}},
{"$sort": bson.M{"name": 1}},
{"$limit": 300}},
)
err = pipe.All(&results)
if err != nil {
panic(err)
}
fmt.Printf("Total results: %d\n", len(results))
// start the server
server := http.Server{
Addr: ":8080",
}
http.HandleFunc("/", index)
server.ListenAndServe()
}
func index(w http.ResponseWriter, r *http.Request) {
t := template.New("main") //name of the template is main
t, _ = t.Parse(tmpl) // parsing of template string
t.Execute(w, struct{ List []personaFields }{results})
}