-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexample_test.go
More file actions
60 lines (47 loc) · 834 Bytes
/
Copy pathexample_test.go
File metadata and controls
60 lines (47 loc) · 834 Bytes
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 lungo
import (
"fmt"
"go.mongodb.org/mongo-driver/bson"
)
func Example() {
type post struct {
Title string `bson:"title"`
}
// prepare options
opts := Options{
Store: NewMemoryStore(),
}
// open database
client, engine, err := Open(nil, opts)
if err != nil {
panic(err)
}
// ensure engine is closed
defer engine.Close()
// get db
foo := client.Database("foo")
// get collection
bar := foo.Collection("bar")
// insert post
_, err = bar.InsertOne(nil, &post{
Title: "Hello World!",
})
if err != nil {
panic(err)
}
// query posts
csr, err := bar.Find(nil, bson.M{})
if err != nil {
panic(err)
}
// decode posts
var posts []post
err = csr.All(nil, &posts)
if err != nil {
panic(err)
}
// print documents
fmt.Printf("%+v", posts)
// Output:
// [{Title:Hello World!}]
}