-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdgo.go
More file actions
60 lines (51 loc) · 1.24 KB
/
bdgo.go
File metadata and controls
60 lines (51 loc) · 1.24 KB
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
60
package main
import (
"fmt"
"context"
"log"
// "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
//"go.mongodb.org/mongo-driver/mongo/readpref"
)
func AddData(data Test, collection *mongo.Collection ) error{
insertResult, err := collection.InsertOne(context.TODO(), data)
if err != nil{
log.Fatal(err)
}
fmt.Printf("value:%v",insertResult)
return err
}
//тестовая структура
type Test struct{
name string
time int32
}
func main(){
addr := "158.101.195.184"
port := 27017
dbName := "hackaton"
collectionName := "weather_log"
client, err := mongo.NewClient(options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%d",addr,port)))
if err != nil{
log.Fatal(err)
}
err = client.Connect(context.TODO())
if err != nil{
log.Fatal(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil{
log.Fatal(err)
}
fmt.Println("Connect to Mongo db is sucsses!")
collection := client.Database(dbName).Collection(collectionName)
fmt.Printf("%T\n",collection)
testData1 := Test{"test1", 42}
err = AddData(testData1, collection)
err = client.Disconnect(context.TODO())
if err != nil{
log.Fatal(err)
}
fmt.Println("Connection with MongoDB close")
}