From aa961a9b5732f9b994f75ff276534ad73bdbbe68 Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Thu, 26 Jan 2023 14:21:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + create.go | 35 +++++++++++++++++++++ go.mod | 2 +- main.go | 27 +++------------- model.go | 18 +++++++++++ query.go | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 23 deletions(-) create mode 100644 create.go create mode 100644 model.go create mode 100644 query.go diff --git a/.gitignore b/.gitignore index f4d432a..fbc25c6 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +mongo-example \ No newline at end of file diff --git a/create.go b/create.go new file mode 100644 index 0000000..98c6bf3 --- /dev/null +++ b/create.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "log" + + "go.mongodb.org/mongo-driver/bson/primitive" + "myschools.me/suguo/mongo-example/mongo" +) + +func create() { + obj, err := mongo.DocumentCreate("person", &Student{ + ID: primitive.NewObjectID(), //ObjectID("000000000000000000000000"),重复插入出现E1100错误 + Name: "王保强", + Sex: "男", + }) + if err != nil { + log.Fatal(err) + } + + fmt.Println(obj) + + obj, err = mongo.DocumentCreate("booking", &Booking{ + ID: primitive.NewObjectID().Hex(), + Uid: 120, + IndustryId: 1, + LocationId: 20, + BaseLocationId: 853, + }) + if err != nil { + log.Fatal(err) + } + + fmt.Println(obj) +} diff --git a/go.mod b/go.mod index 8152d8f..ed3462d 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module myschools.me/suguo/mongo-sample +module myschools.me/suguo/mongo-example go 1.19 diff --git a/main.go b/main.go index fd71a2a..45bace2 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,7 @@ package main import ( - "fmt" - "log" - - "go.mongodb.org/mongo-driver/bson/primitive" - "myschools.me/suguo/mongo-sample/mongo" + "myschools.me/suguo/mongo-example/mongo" ) func main() { @@ -15,21 +11,8 @@ func main() { Timeout: 10, }) - obj, err := mongo.DocumentCreate("person", &Student{ - ID: primitive.NewObjectID(), //ObjectID("000000000000000000000000"),重复插入出现E1100错误 - Name: "王保强", - Sex: "男", - }) - if err != nil { - log.Fatal(err) - } - - fmt.Println(obj) - -} - -type Student struct { - ID primitive.ObjectID `bson:"_id" json:"id,omitempty"` - Name string `json:"name,omitempty"` - Sex string `json:"sex,omitempty"` + // create() + // query() + // queryBooking() + queryResult() } diff --git a/model.go b/model.go new file mode 100644 index 0000000..cb7b578 --- /dev/null +++ b/model.go @@ -0,0 +1,18 @@ +package main + +import "go.mongodb.org/mongo-driver/bson/primitive" + +type Student struct { + ID primitive.ObjectID `bson:"_id" json:"id,omitempty"` + Name string `json:"name,omitempty"` + Sex string `json:"sex,omitempty"` +} + +type Booking struct { + // booking fields + ID interface{} `json:"_id" bson:"_id,omitempty"` + Uid int `json:"uid,omitempty" bson:"uid,omitempty"` + IndustryId int `json:"industry_id,omitempty" bson:"industry_id,omitempty"` + LocationId int `json:"location_id,omitempty" bson:"location_id,omitempty"` + BaseLocationId int `json:"base_location_id,omitempty" bson:"base_location_id,omitempty"` +} diff --git a/query.go b/query.go new file mode 100644 index 0000000..f4e98a4 --- /dev/null +++ b/query.go @@ -0,0 +1,90 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "log" + "time" + + "go.mongodb.org/mongo-driver/bson" + "myschools.me/suguo/mongo-example/mongo" +) + +func query() { + // 使用游标形式查询数据 + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + client, _ := mongo.New() + defer client.Disconnect(ctx) + + collection := client.Database("example").Collection("person") + cur, err := collection.Find(ctx, bson.D{}) + if err != nil { + log.Fatal(err) + } + defer cur.Close(ctx) + for cur.Next(ctx) { + var result bson.D + var r2 Student + err := cur.Decode(&result) + if err != nil { + log.Fatal(err) + } + err = cur.Decode(&r2) + if err != nil { + log.Fatal(err) + } + // do something with result.... + fmt.Println(result) + fmt.Println(r2, r2.ID.Hex()) + } + if err := cur.Err(); err != nil { + log.Fatal(err) + } +} + +func queryBooking() { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + client, _ := mongo.New() + defer client.Disconnect(ctx) + + collection := client.Database("example").Collection("booking") + cur, err := collection.Find(ctx, bson.D{}) + if err != nil { + log.Fatal(err) + } + defer cur.Close(ctx) + + for cur.Next(ctx) { + var book Booking + if err := cur.Decode(&book); err != nil { + log.Fatal(err) + } + + ret, _ := json.Marshal(book) + fmt.Println(string(ret)) + } + + if err := cur.Err(); err != nil { + log.Fatal(err) + } +} + +func queryResult() { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + client, _ := mongo.New() + defer client.Disconnect(ctx) + + collection := client.Database("example").Collection("booking") + book := &Booking{} + if err := collection.FindOne(ctx, bson.M{"_id": "63d20bb744ecbc6b61a283ef"}).Decode(&book); err != nil { + log.Fatal(err) + } + fmt.Println(book) +}