-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmongo_test.go
42 lines (38 loc) · 1.03 KB
/
xmongo_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
package xmongo_test
import (
"context"
"fmt"
"time"
"github.com/caiyunapp/xmongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func ExampleFindOne() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
collection := client.Database(databaseName).Collection(collectionName)
res, err := xmongo.FindOne[Record](ctx, collection, bson.M{})
if err != nil {
panic(err)
}
fmt.Println(res)
}
func ExampleFind() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
collection := client.Database(databaseName).Collection(collectionName)
res, err := xmongo.Find[Record](ctx, collection, bson.M{})
if err != nil {
panic(err)
}
fmt.Println(res)
}