/
db.go
executable file
·47 lines (36 loc) · 1.11 KB
/
db.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
package main
import (
"context"
// "fmt"
"time"
"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"
)
type AutoReplyPost struct {
MsgIn, MsgOut string
}
func Conn(msgin string) string {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + dbUser + ":" + dbPwd + "@localhost:27017/" + dbDb))
if err != nil {
panic(err.Error())
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
panic(err.Error())
}
defer client.Disconnect(ctx)
collection := client.Database("rebbachi").Collection("autoReply")
filter := bson.D{{"MsgIn", msgin}}
var post AutoReplyPost
err = collection.FindOne(context.TODO(), filter).Decode(&post)
// insertResult, err := collection.InsertOne(context.TODO(), Post{Title: "Haha", Body: "heihei"})
// if err != nil {
// panic(err.Error())
// }
// fmt.Println("Inserted post with ID:", insertResult.InsertedID)
// fmt.Println("Found post with MsgIn ", post.MsgIn)
return post.MsgOut
}