/
main.go
53 lines (44 loc) · 1.17 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
package main
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type message struct {
Name string
Age int
}
type MyDoc struct {
SomeInt int `bson:"some_int"`
SomeString string `bson:"some_string,omitempty"`
CustomType MyType `bson:"custom_type,omitempty"`
}
type MyType struct {
Value string `bson:"value,omitempty"`
}
func main() {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
url := fmt.Sprintf("mongodb://%s:%s@%s:27017", "root", "root", "127.0.0.1")
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
if err != nil {
fmt.Println(err)
}
db := client.Database("demo")
fmt.Println("switch database to demo")
// res, err := db.Collection("demo").InsertOne(ctx, bson.D{
// {"task", "1"},
// {"createAt", "2019-04-02"},
// })
msg := &message{"王春雷", 22}
_, err = db.Collection("demo").InsertOne(ctx, msg)
// collection := db.Collection("demo")
// var myType = MyType{Value: "ABCD"}
// docToInsert := MyDoc{42, "The Answer", myType}
// _, err = collection.InsertOne(nil, docToInsert)
if err != nil {
fmt.Println(err)
}
client.Disconnect(nil)
}