更新中
This commit is contained in:
parent
49376c4474
commit
aa961a9b57
|
|
@ -15,3 +15,4 @@
|
|||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
mongo-example
|
||||
|
|
@ -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)
|
||||
}
|
||||
2
go.mod
2
go.mod
|
|
@ -1,4 +1,4 @@
|
|||
module myschools.me/suguo/mongo-sample
|
||||
module myschools.me/suguo/mongo-example
|
||||
|
||||
go 1.19
|
||||
|
||||
|
|
|
|||
27
main.go
27
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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
Loading…
Reference in New Issue