-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_test.go
44 lines (37 loc) · 1.26 KB
/
mongodb_test.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
/*
* @Author: shenruijie [email protected]
* @Date: 2022-10-24 17:46:49
* @LastEditors: shenguanjiejie [email protected]
* @LastEditTime: 2022-10-24 18:40:20
* @FilePath: /mongo-tools/mongodb_test.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package mongotools
import (
"context"
"testing"
"github.com/shenguanjiejie/go-tools"
"go.mongodb.org/mongo-driver/bson"
)
type Person struct {
Name string `json:"name" bson:"name"`
Age int `json:"age" bson:"age"`
}
func TestLog(t *testing.T) {
client, _ := MongoClient("mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000")
col := client.Database("test").Collection("testC")
// col.InsertOne(context.Background(), &Person{"Another", 20})
cursor, err := col.Find(context.Background(), bson.M{})
if err != nil {
tools.Logln(err)
t.Error(err)
return
}
LogCursor(cursor)
}
func TestSingleLog(t *testing.T) {
client, _ := MongoClient("mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000")
col := client.Database("test").Collection("testC")
result := col.FindOne(context.Background(), bson.M{})
LogSingleResult(result)
}