-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
93 lines (81 loc) · 2.19 KB
/
get.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package crud
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
)
func getData() {
// Use the SetServerAPIOptions() method to set the Stable API version to 1
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// Create a new client and connect to the server
client, err := mongo.Connect(ctx, opts)
if err != nil {
panic(err)
}
defer func() {
cancel()
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()
quickStartDB := client.Database("quickstart")
podcastCollection := quickStartDB.Collection("podcasts")
episodesCollection := quickStartDB.Collection("episodes")
cursor, err := episodesCollection.Find(ctx, bson.M{})
if err != nil {
log.Fatal(err)
}
var episodes []bson.M
if err = cursor.All(ctx, &episodes); err != nil {
log.Fatal(err)
}
fmt.Println(episodes)
// Get all the elements of episodes collection
cursor, err = episodesCollection.Find(ctx, bson.M{})
if err != nil {
log.Fatal(err)
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var episode bson.M
if err = cursor.Decode(&episode); err != nil {
log.Fatal(err)
}
fmt.Println(episode)
}
// Get one element of PodCase collection
var podcast bson.M
if err = podcastCollection.FindOne(ctx, bson.M{}).Decode(&podcast); err != nil {
log.Fatal(err)
}
fmt.Println(podcast)
// Get episodes whose duration == 25
filterCursor, err := episodesCollection.Find(ctx, bson.M{"duration": 25})
if err != nil {
log.Fatal(err)
}
var episodesFiltered []bson.M
if err = filterCursor.All(ctx, &episodesFiltered); err != nil {
log.Fatal(err)
}
//fmt.Println(episodesFiltered)
// sort episodes and filter it based on the duration field
opt := options.Find()
opt.SetSort(bson.D{{"duration", 1}})
sortCursor, err := episodesCollection.Find(ctx, bson.D{
{"duration", bson.D{
{"$gt", 24},
}},
}, opt)
var episodesSorted []bson.M
if err = sortCursor.All(ctx, &episodesSorted); err != nil {
log.Fatal(err)
}
fmt.Println(episodesSorted)
}