Skip to content

Commit f1d393a

Browse files
Some mongodb experiments
Signed-off-by: Rohit Nayak <[email protected]>
1 parent f18f07b commit f1d393a

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

vr-mongo/connect.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"go.mongodb.org/mongo-driver/bson"
7+
"go.mongodb.org/mongo-driver/mongo"
8+
"go.mongodb.org/mongo-driver/mongo/options"
9+
"time"
10+
)
11+
12+
func main() {
13+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
14+
defer cancel()
15+
client, err := mongo.Connect(ctx, options.Client().ApplyURI(
16+
"mongodb://test:[email protected]/test?retryWrites=true&w=majority",
17+
))
18+
if err != nil {
19+
fmt.Println(err.Error())
20+
return
21+
}
22+
//fmt.Printf("client %v:%s\n", client, err)
23+
24+
collection := client.Database("test").Collection("inventory")
25+
count, err := collection.CountDocuments(ctx, bson.D{{}}, nil)
26+
if err != nil {
27+
fmt.Println(err.Error())
28+
return
29+
}
30+
fmt.Printf("#docs %d\n", count)
31+
}

vr-mongo/logtail.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/gnokoheat/oplog"
6+
)
7+
8+
func main() {
9+
var o = &oplog.Options{
10+
// "mongodb+srv://rohit:[email protected]/sample_airbnb?retryWrites=true&w=majority",
11+
// (e.g. mongodb://username:[email protected]:27017,127.0.0.1:27018/local?replicaSet=rs01&authSource=admin)
12+
Addrs: []string{"127.0.0.1"}, // replicaset host and port
13+
Username: "rohit", // admin db username
14+
Password: "abc123", // admin db user password
15+
ReplicaSet: "rs", // replicaset name
16+
DB: "test", // tailing target db
17+
Collection: "inventory", // tailing target collection
18+
Events: []string{"insert", "update", "delete"}, // tailing target method
19+
}
20+
21+
l := make(chan *[]oplog.Log) // Oplog Channel
22+
e := make(chan error) // Error Channel
23+
fmt.Printf("o is %v\n", o)
24+
25+
// Oplog tailing start !
26+
go o.Tail(l, e)
27+
28+
for {
29+
select {
30+
case err := <-e:
31+
fmt.Printf("[Error] %s\n", err)
32+
return
33+
case op := <-l:
34+
// input oplog handling code
35+
fmt.Printf("[Result] %v\n", op)
36+
break
37+
}
38+
}
39+
}

vr-mongo/x.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/rwynn/gtm/v2"
7+
"go.mongodb.org/mongo-driver/bson"
8+
"go.mongodb.org/mongo-driver/bson/bsontype"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
11+
"reflect"
12+
"time"
13+
)
14+
15+
func main() {
16+
rb := bson.NewRegistryBuilder()
17+
//rb.RegisterTypeMapEntry(bsontype.Timestamp, reflect.TypeOf(time.Time{}))
18+
rb.RegisterTypeMapEntry(bsontype.DateTime, reflect.TypeOf(time.Time{}))
19+
reg := rb.Build()
20+
clientOptions := options.Client()
21+
clientOptions.SetRegistry(reg)
22+
clientOptions.ApplyURI("mongodb://localhost:27017")
23+
client, err := mongo.NewClient(clientOptions)
24+
if err != nil {
25+
panic(err)
26+
}
27+
ctxm, cancel := context.WithTimeout(context.Background(), 20*time.Second)
28+
defer cancel()
29+
err = client.Connect(ctxm)
30+
if err != nil {
31+
panic(err)
32+
}
33+
defer client.Disconnect(context.Background())
34+
ctx := gtm.Start(client, &gtm.Options{
35+
DirectReadNs: []string{"test.test"},
36+
ChangeStreamNs: []string{"test.test"},
37+
//MaxWaitSecs: 10,
38+
OpLogDisabled: true,
39+
})
40+
for {
41+
select {
42+
case err := <-ctx.ErrC:
43+
fmt.Printf("got err %+v", err)
44+
break
45+
case op := <-ctx.OpC:
46+
fmt.Printf("got op %+v", op)
47+
break
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)