/
main.go
59 lines (52 loc) · 1.94 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
package main
import (
"context"
"fmt"
"github.com/pubgo/xerror"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"os"
"time"
)
func main() {
fmt.Println(os.Getenv("mongodb"))
clientOptions := options.Client().ApplyURI(os.Getenv("mongodb"))
c, err := mongo.NewClient(clientOptions)
xerror.Panic(err)
xerror.Panic(c.Connect(context.Background()))
//cc := c.Database("test_hello").
// Collection("test_hello", options.Collection().SetReadPreference(readpref.SecondaryPreferred()))
// Majority read concern requested, but server was not started with --enableMajorityReadConcern. 28ms
cc := c.Database("test_hello").
Collection("test_hello", options.Collection().
//SetReadPreference(readpref.PrimaryPreferred()).
SetReadPreference(readpref.SecondaryPreferred()).
SetWriteConcern(writeconcern.New(writeconcern.W(3)))) //writeconcern.WTimeout(2*time.Second)
//SetWriteConcern(writeconcern.New(writeconcern.WMajority())).
//SetReadConcern(readconcern.Majority()),
//writeconcern.WTimeout(2*time.Second)
//cc := c.Database("test_hello").Collection("test_hello")
//cc1 := c.Database("test_hello").Collection("test_hello")
for i := 0; ; i++ {
i := i
now := time.Now()
dd, err := cc.InsertOne(context.Background(), bson.M{"foo": "bar", "hello": "world", "pi": 3.14159, "time": time.Now()})
xerror.Panic(err)
fmt.Println(dd.InsertedID.(primitive.ObjectID).Hex())
//time.Sleep(time.Millisecond * 200)
fmt.Println(time.Since(now))
var data bson.M
err = cc.FindOne(context.Background(), bson.M{"_id": dd.InsertedID}).Decode(&data)
if err != nil {
fmt.Println(err.Error(), time.Millisecond*time.Duration(i))
return
}
fmt.Println(time.Since(now))
fmt.Println(data)
}
// 先从SecondaryPreferred读再从PrimaryPreferred读
}