-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
133 lines (114 loc) · 3.63 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Podcast struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Title string `bson:"title,omitempty"`
Author string `bson:"author,omitempty"`
Tags []string `bson:"tags,omitempty"`
}
type Episode struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Podcast primitive.ObjectID `bson:"podcast,omitempty"`
Title string `bson:"title,omitempty"`
Description string `bson:"description,omitempty"`
Duration int32 `bson:"duration,omitempty"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mongoUri := fetchMongoUri(ctx)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoUri))
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
fmt.Println("Connected.")
database := client.Database(os.Getenv("TIGRIS_PROJECT"))
podcastsCollection := database.Collection("podcasts")
episodesCollection := database.Collection("episodes")
// InsertOne
podcastResult, err := podcastsCollection.InsertOne(ctx, Podcast{
Title: "The Polyglot Developer",
Author: "Nic Raboy",
Tags: []string{"development", "programming", "coding"},
})
if err != nil {
panic(err)
}
podcastID := podcastResult.InsertedID.(primitive.ObjectID)
fmt.Printf("Inserted document into podcast collection: %v\n", podcastID.String())
// InsertMany
episodeResult, err := episodesCollection.InsertMany(ctx, []interface{}{
Episode{
Podcast: podcastID,
Title: "GraphQL for API Development",
Description: "Learn about GraphQL from the co-creator of GraphQL, Lee Byron.",
Duration: 25,
},
Episode{
Podcast: podcastID,
Title: "Progressive Web Application Development",
Description: "Learn about PWA development with Tara Manicsic.",
Duration: 32,
},
})
if err != nil {
panic(err)
}
fmt.Printf("Inserted %v documents into episode collection!\n", len(episodeResult.InsertedIDs))
// Find
var episodes []Episode
cursor, err := episodesCollection.Find(ctx, bson.M{"duration": bson.D{{"$gt", 25}}})
if err != nil {
panic(err)
}
if err = cursor.All(ctx, &episodes); err != nil {
panic(err)
}
jsonData, err := json.MarshalIndent(episodes, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found %v documents matching filter!\n", len(episodes))
fmt.Printf("%s\n", jsonData)
// Update
updateResult, err := podcastsCollection.UpdateOne(
ctx,
bson.M{"_id": podcastID},
bson.D{
{"$set", bson.D{{"title", "The Polyglot Developer Podcast"}}},
})
if err != nil {
panic(err)
}
fmt.Printf("Updated %v Documents!\n", updateResult.ModifiedCount)
// Delete
deleteResult, err := episodesCollection.DeleteOne(ctx, bson.D{{"title", "GraphQL for API Development"}})
if err != nil {
panic(err)
}
fmt.Printf("DeleteOne removed %v document(s)\n", deleteResult.DeletedCount)
}
func fetchMongoUri(ctx context.Context) string {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
uri := os.Getenv("TIGRIS_URI")
clientId := os.Getenv("TIGRIS_CLIENT_ID")
clientSecret := os.Getenv("TIGRIS_CLIENT_SECRET")
if uri == "" || clientId == "" || clientSecret == "" {
log.Fatal("You must set the 'TIGRIS_URI', 'TIGRIS_CLIENT_ID', and 'TIGRIS_CLIENT_SECRET' environment variables.")
}
return fmt.Sprintf("mongodb://%s:%s@%s/?authMechanism=PLAIN&tls=true", clientId, clientSecret, uri)
}