Skip to content

Commit 663580e

Browse files
DOCSP-19088 code snippet updates (#70)
* updated all code snippets
1 parent 506aea9 commit 663580e

File tree

26 files changed

+304
-294
lines changed

26 files changed

+304
-294
lines changed

source/fundamentals/crud/read-operations/sort.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ The following example specifies an ascending sort on the ``rating`` field:
7474
:dedent:
7575
:start-after: begin ascending sort
7676
:end-before: end ascending sort
77-
:emphasize-lines: 2-3, 5
7877

7978
After running this example, the output resembles the following:
8079

@@ -111,7 +110,6 @@ The following example specifies a descending sort on the ``rating`` field:
111110
:dedent:
112111
:start-after: begin descending sort
113112
:end-before: end descending sort
114-
:emphasize-lines: 2-3, 5
115113

116114
After running this example, the output resembles the following:
117115

@@ -154,7 +152,6 @@ then a descending sort on the ``type`` field:
154152
:dedent:
155153
:start-after: begin multi sort
156154
:end-before: end multi sort
157-
:emphasize-lines: 2-3, 5
158155

159156
After running this example, the output resembles the following:
160157

source/fundamentals/crud/write-operations/change-a-document.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ For more information on update operators,
287287
see the :manual:`MongoDB update operator reference documentation
288288
</reference/operator/update/#update-operators>`.
289289

290-
For runnable examples of the update operations, see the following usage
291-
examples:
290+
For runnable examples of the update and replace operations, see the
291+
following usage examples:
292292

293293
- :doc:`Update a Document </usage-examples/updateOne>`
294294
- :doc:`Update Multiple Documents </usage-examples/updateMany>`

source/includes/fundamentals/code-snippets/CRUD/compoundOperations.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func main() {
1515
var uri string
1616
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17-
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")
1818
}
1919

2020
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
@@ -40,9 +40,9 @@ func main() {
4040
bson.D{{"type", "English Breakfast"}, {"rating", 5}},
4141
}
4242

43-
result, insertErr := coll.InsertMany(context.TODO(), docs)
44-
if insertErr != nil {
45-
panic(insertErr)
43+
result, err := coll.InsertMany(context.TODO(), docs)
44+
if err != nil {
45+
panic(err)
4646
}
4747
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
4848
//end insertDocs

source/includes/fundamentals/code-snippets/CRUD/delete.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func main() {
1515
var uri string
1616
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17-
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")
1818
}
1919

2020
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
@@ -39,21 +39,25 @@ func main() {
3939
bson.D{{"type", "Assam"}, {"rating", 7}},
4040
}
4141

42-
result, insertErr := coll.InsertMany(context.TODO(), docs)
43-
if insertErr != nil {
44-
panic(insertErr)
42+
result, err := coll.InsertMany(context.TODO(), docs)
43+
if err != nil {
44+
panic(err)
4545
}
4646
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
4747
// end insertDocs
4848

49-
// begin deleteMany
50-
deleteManyFilter := bson.D{{"rating", bson.D{{"$gt", 8}}}}
51-
deleteOptions := options.Delete().SetHint(bson.D{{"_id", 1}})
49+
fmt.Println("Delete Many:")
50+
{
51+
// begin deleteMany
52+
filter := bson.D{{"rating", bson.D{{"$gt", 8}}}}
53+
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
54+
55+
result, err := coll.DeleteMany(context.TODO(), filter, opts)
56+
if err != nil {
57+
panic(err)
58+
}
5259

53-
deleteManyResult, deleteManyErr := coll.DeleteMany(context.TODO(), deleteManyFilter, deleteOptions)
54-
fmt.Printf("Number of documents deleted: %d\n", deleteManyResult.DeletedCount)
55-
// end deleteMany
56-
if deleteManyErr != nil {
57-
panic(deleteManyErr)
60+
fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)
61+
// end deleteMany
5862
}
5963
}

source/includes/fundamentals/code-snippets/CRUD/limit.go

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func main() {
1515
var uri string
1616
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17-
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")
1818
}
1919

2020
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
@@ -40,60 +40,66 @@ func main() {
4040
bson.D{{"type", "English Breakfast"}, {"rating", 5}},
4141
}
4242

43-
result, insertErr := coll.InsertMany(context.TODO(), docs)
44-
if insertErr != nil {
45-
panic(insertErr)
43+
result, err := coll.InsertMany(context.TODO(), docs)
44+
if err != nil {
45+
panic(err)
4646
}
4747
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
4848
//end insertDocs
4949

5050
fmt.Println("Limit:")
51-
//begin limit
52-
limitFilter := bson.D{}
53-
limitOptions := options.Find().SetLimit(2)
51+
{
52+
//begin limit
53+
filter := bson.D{}
54+
opts := options.Find().SetLimit(2)
5455

55-
limitCursor, limitErr := coll.Find(context.TODO(), limitFilter, limitOptions)
56+
cursor, err := coll.Find(context.TODO(), filter, opts)
5657

57-
var limitResults []bson.D
58-
if limitErr = limitCursor.All(context.TODO(), &limitResults); limitErr != nil {
59-
panic(limitErr)
60-
}
61-
for _, result := range limitResults {
62-
fmt.Println(result)
58+
var results []bson.D
59+
if err = cursor.All(context.TODO(), &results); err != nil {
60+
panic(err)
61+
}
62+
for _, result := range results {
63+
fmt.Println(result)
64+
}
65+
//end limit
6366
}
64-
//end limit
6567

6668
fmt.Println("Limit, Skip and Sort:")
67-
//begin multi options
68-
multiFilter := bson.D{}
69-
multiOptions := options.Find().SetSort(bson.D{{"rating", -1}}).SetLimit(2).SetSkip(1)
69+
{
70+
//begin multi options
71+
filter := bson.D{}
72+
opts := options.Find().SetSort(bson.D{{"rating", -1}}).SetLimit(2).SetSkip(1)
7073

71-
multiCursor, multiErr := coll.Find(context.TODO(), multiFilter, multiOptions)
74+
cursor, err := coll.Find(context.TODO(), filter, opts)
7275

73-
var multiResults []bson.D
74-
if multiErr = multiCursor.All(context.TODO(), &multiResults); multiErr != nil {
75-
panic(multiErr)
76-
}
77-
for _, result := range multiResults {
78-
fmt.Println(result)
76+
var results []bson.D
77+
if err = cursor.All(context.TODO(), &results); err != nil {
78+
panic(err)
79+
}
80+
for _, result := range results {
81+
fmt.Println(result)
82+
}
83+
//end multi options
7984
}
80-
//end multi options
8185

8286
fmt.Println("Aggregation Limit:")
83-
// begin aggregate limit
84-
limitStage := bson.D{{"$limit", 3}}
87+
{
88+
// begin aggregate limit
89+
limitStage := bson.D{{"$limit", 3}}
8590

86-
aggCursor, aggErr := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
87-
if aggErr != nil {
88-
panic(aggErr)
89-
}
91+
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
92+
if err != nil {
93+
panic(err)
94+
}
9095

91-
var aggResults []bson.D
92-
if aggErr = aggCursor.All(context.TODO(), &aggResults); aggErr != nil {
93-
panic(aggErr)
94-
}
95-
for _, result := range aggResults {
96-
fmt.Println(result)
96+
var results []bson.D
97+
if err = cursor.All(context.TODO(), &results); err != nil {
98+
panic(err)
99+
}
100+
for _, result := range results {
101+
fmt.Println(result)
102+
}
103+
// end aggregate limit
97104
}
98-
// end aggregate limit
99-
}
105+
}

source/includes/fundamentals/code-snippets/CRUD/retrieve.go

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func main() {
1515
var uri string
1616
if uri = os.Getenv("DRIVER_REF_URI"); uri == "" {
17-
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/#environment-variable")
1818
}
1919

2020
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
@@ -43,76 +43,78 @@ func main() {
4343
if err != nil {
4444
panic(err)
4545
}
46-
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs))
46+
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
4747
// end insert docs
4848

49-
for _, id := range result.InsertedIDs {
50-
fmt.Printf("\t%s\n", id)
51-
}
52-
5349
fmt.Println("Find:")
54-
// begin find docs
55-
findFilter := bson.D{
56-
{"$and",
57-
bson.A{
58-
bson.D{{"rating", bson.D{{"$gt", 5}}}},
59-
bson.D{{"rating", bson.D{{"$lt", 10}}}},
60-
}},
61-
}
62-
findProjection := bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}
63-
findOptions := options.Find().SetProjection(findProjection)
50+
{
51+
// begin find docs
52+
filter := bson.D{
53+
{"$and",
54+
bson.A{
55+
bson.D{{"rating", bson.D{{"$gt", 5}}}},
56+
bson.D{{"rating", bson.D{{"$lt", 10}}}},
57+
}},
58+
}
59+
projection := bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}
60+
opts := options.Find().SetProjection(projection)
6461

65-
findCursor, findErr := coll.Find(context.TODO(), findFilter, findOptions)
66-
if findErr != nil {
67-
panic(findErr)
68-
}
62+
cursor, err := coll.Find(context.TODO(), filter, opts)
63+
if err != nil {
64+
panic(err)
65+
}
6966

70-
var findResults []bson.D
71-
if findErr = findCursor.All(context.TODO(), &findResults); findErr != nil {
72-
panic(findErr)
73-
}
74-
for _, result := range findResults {
75-
fmt.Println(result)
67+
var results []bson.D
68+
if err = cursor.All(context.TODO(), &results); err != nil {
69+
panic(err)
70+
}
71+
for _, result := range results {
72+
fmt.Println(result)
73+
}
74+
// end find docs
7675
}
77-
// end find docs
7876

7977
fmt.Println("Find One:")
80-
// begin find one docs
81-
findOneFilter := bson.D{}
82-
findOnesort := bson.D{{"rating", -1}}
83-
findOneprojection := bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}
84-
findOneOptions := options.FindOne().SetSort(findOnesort).SetProjection(findOneprojection)
85-
86-
var findOneResult bson.D
87-
findOneErr := coll.FindOne(context.TODO(), findOneFilter, findOneOptions).Decode(&findOneResult)
88-
if findOneErr != nil {
89-
panic(findOneErr)
78+
{
79+
// begin find one docs
80+
filter := bson.D{}
81+
sort := bson.D{{"rating", -1}}
82+
projection := bson.D{{"type", 1}, {"rating", 1}, {"_id", 0}}
83+
opts := options.FindOne().SetSort(sort).SetProjection(projection)
84+
85+
var result bson.D
86+
err := coll.FindOne(context.TODO(), filter, opts).Decode(&result)
87+
if err != nil {
88+
panic(err)
89+
}
90+
91+
fmt.Println(result)
92+
// end find one docs
9093
}
91-
92-
fmt.Println(findOneResult)
93-
// end find one docs
9494

9595
fmt.Println("Aggregation:")
96-
// begin aggregate docs
97-
groupStage := bson.D{
98-
{"$group", bson.D{
99-
{"_id", "$type"},
100-
{"average", bson.D{
101-
{"$avg", "$rating"},
102-
}},
103-
}}}
104-
105-
aggCursor, aggErr := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage})
106-
if aggErr != nil {
107-
panic(aggErr)
108-
}
96+
{
97+
// begin aggregate docs
98+
groupStage := bson.D{
99+
{"$group", bson.D{
100+
{"_id", "$type"},
101+
{"average", bson.D{
102+
{"$avg", "$rating"},
103+
}},
104+
}}}
105+
106+
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{groupStage})
107+
if err != nil {
108+
panic(err)
109+
}
109110

110-
var aggResults []bson.M
111-
if aggErr = aggCursor.All(context.TODO(), &aggResults); aggErr != nil {
112-
panic(aggErr)
113-
}
114-
for _, result := range aggResults {
115-
fmt.Printf("%v has an average rating of %v \n", result["_id"], result["average"])
111+
var results []bson.M
112+
if err = cursor.All(context.TODO(), &results); err != nil {
113+
panic(err)
114+
}
115+
for _, result := range results {
116+
fmt.Printf("%v has an average rating of %v \n", result["_id"], result["average"])
117+
}
118+
// end aggregate docs
116119
}
117-
// end aggregate docs
118120
}

0 commit comments

Comments
 (0)