This repository was archived by the owner on Nov 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.go
More file actions
197 lines (162 loc) · 5.98 KB
/
main.go
File metadata and controls
197 lines (162 loc) · 5.98 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// A Recipe Struct allows you to insert recipe documents into your
// collection
type Recipe struct {
Name string
Ingredients []string
PrepTimeInMinutes int `json:"prepTimeInMinutes" bson:"prepTimeInMinutes"`
}
func main() {
// TODO:
// Replace the placeholder connection string below with your
// Atlas cluster specifics. Be sure it includes
// a valid username and password! Note that in a production environment,
// you do not want to store your password in plain-text here.
var mongoUri = "<Your Atlas Connection String>"
// CONNECT TO YOUR ATLAS CLUSTER:
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(
mongoUri,
))
defer func() {
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()
err = client.Ping(ctx, nil)
if err != nil {
fmt.Println("There was a problem connecting to your Atlas cluster. Check that the URI includes a valid username and password, and that your IP address has been added to the access list. Error: ")
panic(err)
}
fmt.Println("Connected to MongoDB!\n")
// Provide the name of the database and collection you want to use.
// If they don't already exist, the driver and Atlas will create them
// automatically when you first write data.
var dbName = "myDatabase"
var collectionName = "recipes"
collection := client.Database(dbName).Collection(collectionName)
/* *** INSERT DOCUMENTS ***
*
* You can insert individual documents using collection.Insert().
* In this example, we're going to create 4 documents and then
* insert them all in one call with InsertMany().
*/
eloteRecipe := Recipe{
Name: "elote",
Ingredients: []string{"corn", "mayonnaise", "cotija cheese", "sour cream", "lime"},
PrepTimeInMinutes: 35,
}
locoMocoRecipe := Recipe{
Name: "loco moco",
Ingredients: []string{"ground beef", "butter", "onion", "egg", "bread bun", "mushrooms"},
PrepTimeInMinutes: 54,
}
patatasBravasRecipe := Recipe{
Name: "patatas bravas",
Ingredients: []string{"potato", "tomato", "olive oil", "onion", "garlic", "paprika"},
PrepTimeInMinutes: 80,
}
friedRiceRecipe := Recipe{
Name: "fried rice",
Ingredients: []string{"rice", "soy sauce", "egg", "onion", "pea", "carrot", "sesame oil"},
PrepTimeInMinutes: 40,
}
// Create an interface of all the created recipes
recipes := []interface{}{eloteRecipe, locoMocoRecipe, patatasBravasRecipe, friedRiceRecipe}
insertManyResult, err := collection.InsertMany(context.TODO(), recipes)
if err != nil {
fmt.Println("Something went wrong trying to insert the new documents:")
panic(err)
}
fmt.Println(len(insertManyResult.InsertedIDs), "documents successfully inserted.\n")
/*
* *** FIND DOCUMENTS ***
*
* Now that we have data in Atlas, we can read it. To retrieve all of
* the data in a collection, we create a filter for recipes that take
* less than 45 minutes to prepare and sort by name (ascending)
*/
var filter = bson.M{"prepTimeInMinutes": bson.M{"$lt": 45}}
options := options.Find()
// Sort by `name` field ascending
options.SetSort(bson.D{{"name", 1}})
cursor, err := collection.Find(context.TODO(), filter, options)
if err != nil {
fmt.Println("Something went wrong trying to find the documents:")
panic(err)
}
defer func() {
cursor.Close(context.Background())
}()
// Loop through the returned recipes
for cursor.Next(ctx) {
recipe := Recipe{}
err := cursor.Decode(&recipe)
// If there is an error decoding the cursor into a Recipe
if err != nil {
fmt.Println("cursor.Next() error:")
panic(err)
} else {
fmt.Println(recipe.Name, "has", len(recipe.Ingredients), "ingredients, and takes", recipe.PrepTimeInMinutes, "minutes to make.\n")
}
}
// We can also find a single document. Let's find the first document
// that has the string "potato" as an ingredient
var result Recipe
var myFilter = bson.D{{"ingredients", "potato"}}
e := collection.FindOne(context.TODO(), myFilter).Decode(&result)
if e != nil {
fmt.Println("Something went wrong trying to find one document:")
panic(e)
}
fmt.Println("Found a document with the ingredient potato", result, "\n")
/*
* *** UPDATE A DOCUMENT ***
*
* You can update a single document or multiple documents in a single call.
*
* Here we update the prepTimeInMinutes value on the document we
* just found.
*/
var updateDoc = bson.D{{"$set", bson.D{{"prepTimeInMinutes", 72}}}}
var myRes = collection.FindOneAndUpdate(ctx, myFilter, updateDoc, nil)
if myRes.Err() != nil {
fmt.Println("Something went wrong trying to update one document:")
panic(myRes.Err())
}
_recipe := Recipe{}
decodeErr := myRes.Decode(&_recipe)
if decodeErr != nil {
fmt.Println("Something went wrong trying to decode the document:")
panic(decodeErr)
}
// indent the Recipe output to cleanly print the document using json.MarshallIndent
updatedRecipe, _ := json.MarshalIndent(_recipe, "", "\t")
fmt.Println("The following document has been updated: \n", string(updatedRecipe), "\n")
/* *** DELETE DOCUMENTS ***
*
* As with other CRUD methods, you can delete a single document
* or all documents that match a specified filter. To delete all
* of the documents in a collection, pass an empty filter to
* the DeleteMany() method. In this example, we'll delete two of
* the recipes.
*/
deletedRecipeNameList := [...]string{"elote", "fried rice"}
var deleteQuery = bson.M{"name": bson.M{"$in": deletedRecipeNameList}}
deleteResult, err := collection.DeleteMany(context.TODO(), deleteQuery)
if err != nil {
fmt.Println("Something went wrong trying to delete documents:")
panic(err)
}
fmt.Println("Deleted", deleteResult.DeletedCount, "documents in the recipes collection\n")
}