Skip to content

Commit

Permalink
polish example mongo code
Browse files Browse the repository at this point in the history
  • Loading branch information
UnderTreeTech committed Aug 31, 2021
1 parent a8c4c31 commit 27db024
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/mongo/application.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[mongo]
dbName = "waterdrop"
addr = "127.0.0.1:27017"
dsn = "mongodb://user:password@127.0.0.1:27017/?connect=direct"
dsn = "mongodb://root:123456@127.0.0.1:27017/?connect=direct"
#specifies that maximum number of connections allowed in the driver's connection pool to each server.
maxPoolSize = 100
#specifies the minimum number of connections allowed in the driver's connection pool to each server. If
Expand Down
18 changes: 8 additions & 10 deletions examples/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (

"github.com/UnderTreeTech/waterdrop/pkg/log"

"go.mongodb.org/mongo-driver/bson"

"github.com/UnderTreeTech/waterdrop/pkg/trace/jaeger"

"github.com/UnderTreeTech/waterdrop/pkg/database/mongo"
Expand Down Expand Up @@ -150,7 +148,7 @@ func testMongo(c *gin.Context) {

// find one
var userinfo *UserInfo
err = db.GetCollection("user").Find(ctx, bson.M{"name": user.Name}).One(&userinfo)
err = db.GetCollection("user").Find(ctx, mongo.M{"name": user.Name}).One(&userinfo)
if err != nil {
fmt.Println("query doc fail", err)
} else {
Expand All @@ -159,7 +157,7 @@ func testMongo(c *gin.Context) {

// find all
allUsers := make([]*UserInfo, 0)
err = db.GetCollection("user").Find(ctx, bson.M{}).Sort("name").Limit(5).All(&allUsers)
err = db.GetCollection("user").Find(ctx, mongo.M{}).Sort("name").Limit(5).All(&allUsers)
if err != nil {
fmt.Println("query limit docs fail", err)
} else {
Expand All @@ -169,23 +167,23 @@ func testMongo(c *gin.Context) {
}

// count
count, err := db.GetCollection("user").Find(ctx, bson.M{}).Count()
count, err := db.GetCollection("user").Find(ctx, mongo.M{}).Count()
if err != nil {
fmt.Println("count fail", err)
} else {
fmt.Println("count number", count)
}

// update
uret, err := db.GetCollection("user").UpdateAll(ctx, bson.M{"id": bson.M{"$in": []int64{1611138298337130000, 1611138298495058000}}}, bson.M{"$set": bson.M{"email": xstring.RandomString(32)}})
uret, err := db.GetCollection("user").UpdateAll(ctx, mongo.M{"id": mongo.M{"$in": []int64{1611138298337130000, 1611138298495058000}}}, mongo.M{"$set": mongo.M{"email": xstring.RandomString(32)}})
if err != nil {
fmt.Println("update doc fail", err)
} else {
fmt.Println(uret.MatchedCount, uret.ModifiedCount)
}

// delete
err = db.GetCollection("user").Remove(ctx, bson.M{"id": 1611138298337130000})
err = db.GetCollection("user").Remove(ctx, mongo.M{"id": 1611138298337130000})
if err != nil {
fmt.Println("delete doc fail", err)
}
Expand All @@ -207,8 +205,8 @@ func testMongo(c *gin.Context) {

bret, err := db.GetCollection("user").Bulk(ctx).
InsertOne(insert).
UpdateOne(bson.M{"id": 1611138298495058000}, bson.M{"$set": bson.M{"email": xstring.RandomString(32)}}).
Remove(bson.M{"id": 1611138298495401000}).
UpdateOne(mongo.M{"id": 1611138298495058000}, mongo.M{"$set": mongo.M{"email": xstring.RandomString(32)}}).
Remove(mongo.M{"id": 1611138298495401000}).
Run()

if err != nil {
Expand All @@ -217,5 +215,5 @@ func testMongo(c *gin.Context) {
fmt.Println(bret.ModifiedCount, bret.InsertedCount, bret.DeletedCount)
}

c.JSON(200, err.Error())
c.JSON(200, "success")
}

0 comments on commit 27db024

Please sign in to comment.