-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathargos.go
47 lines (38 loc) · 1.05 KB
/
argos.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
// Copyright 2018 Kuei-chun Chen. All rights reserved.
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/simagix/mongo-go-examples/examples"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
)
func main() {
var err error
var client *mongo.Client
var connStr connstring.ConnString
collection := flag.String("collection", "", "collection to watch")
pipe := flag.String("pipeline", "", "aggregation pipeline")
flag.Parse()
flagset := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { flagset[f.Name] = true })
if client, err = examples.GetMongoClient(flag.Arg(0)); err != nil {
panic(err)
}
var pipeline = []bson.D{}
if *pipe != "" {
pipeline = examples.MongoPipeline(*pipe)
}
stream := examples.NewChangeStream()
stream.SetCollection(*collection)
stream.SetDatabase(connStr.Database)
stream.SetPipeline(pipeline)
stream.Watch(client, echo)
}
func echo(doc bson.M) {
var b []byte
b, _ = json.MarshalIndent(doc, "", " ")
fmt.Println(string(b))
}