Skip to content

Commit 128f1e2

Browse files
committed
DOCSP-34174: codewhisperer pt 1 (#320)
* DOCSP-34174: codewhisperer pt 1 * add comments * NR PR fixes 1 * fix index (cherry picked from commit 77557c6)
1 parent d1498d1 commit 128f1e2

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-9
lines changed

source/includes/fact-environments.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
- `MongoDB Atlas
2-
<https://www.mongodb.com/docs/atlas>`__: The fully
2+
<https://www.mongodb.com/docs/atlas>`__: the fully
33
managed service for MongoDB deployments in the cloud
4-
- :ref:`MongoDB Enterprise <install-mdb-enterprise>`: The
4+
- :ref:`MongoDB Enterprise <install-mdb-enterprise>`: the
55
subscription-based, self-managed version of MongoDB
6-
- :ref:`MongoDB Community <install-mdb-community-edition>`: The
6+
- :ref:`MongoDB Community <install-mdb-community-edition>`: the
77
source-available, free-to-use, and self-managed version of MongoDB

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Limits the number of documents returned by a query
12
package main
23

34
import (
@@ -56,10 +57,15 @@ func main() {
5657

5758
fmt.Println("\nLimit:\n")
5859
{
60+
// Creates a filter to match documents that have an
61+
// "enrollment" value greater than 20
5962
//begin limit
6063
filter := bson.D{{"enrollment", bson.D{{"$gt", 20}}}}
64+
65+
// Sets a limit to return the first 2 matched documents
6166
opts := options.Find().SetLimit(2)
6267

68+
// Retrieves documents that match the filter and prints them as structs
6369
cursor, err := coll.Find(context.TODO(), filter, opts)
6470

6571
var results []Course
@@ -75,10 +81,15 @@ func main() {
7581

7682
fmt.Println("\nLimit, Skip, and Sort:\n")
7783
{
84+
// Creates an empty filter to match all documents
7885
//begin multi options
7986
filter := bson.D{}
87+
88+
// Sets options to sort by descending order on "enrollment",
89+
// return only 2 documents, and skip the first matched document
8090
opts := options.Find().SetSort(bson.D{{"enrollment", -1}}).SetLimit(2).SetSkip(1)
8191

92+
// Retrieves documents that match the filter and prints them as structs
8293
cursor, err := coll.Find(context.TODO(), filter, opts)
8394

8495
var results []Course
@@ -94,9 +105,11 @@ func main() {
94105

95106
fmt.Println("\nAggregation Limit:\n")
96107
{
108+
// Creates a limit stage to return 3 documents
97109
// begin aggregate limit
98110
limitStage := bson.D{{"$limit", 3}}
99111

112+
// Aggregates results and prints them as structs
100113
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
101114
if err != nil {
102115
panic(err)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Specifies which document fields to return
12
package main
23

34
import (
@@ -54,10 +55,15 @@ func main() {
5455

5556
fmt.Println("\nExclude Projection:\n")
5657
{
58+
// Creates an empty filter to match all documents
5759
//begin exclude projection
5860
filter := bson.D{}
61+
62+
// Sets a projection to exclude the "course_id" and "enrollment" fields
5963
opts := options.Find().SetProjection(bson.D{{"course_id", 0}, {"enrollment", 0}})
6064

65+
// Retrieves all documents and prints them as structs without
66+
// the specified fields
6167
cursor, err := coll.Find(context.TODO(), filter, opts)
6268
if err != nil {
6369
panic(err)
@@ -76,10 +82,15 @@ func main() {
7682

7783
fmt.Println("\nInclude Projection:\n")
7884
{
85+
// Creates an empty filter to match all documents
7986
//begin include projection
8087
filter := bson.D{}
88+
89+
// Sets a projection to include the "title" and "enrollment" fields
8190
opts := options.Find().SetProjection(bson.D{{"title", 1}, {"enrollment", 1}})
8291

92+
// Retrieves all documents and prints them as structs including
93+
// only the specified fields
8394
cursor, err := coll.Find(context.TODO(), filter, opts)
8495
if err != nil {
8596
panic(err)
@@ -98,9 +109,13 @@ func main() {
98109

99110
fmt.Println("\nAggregation Projection:\n")
100111
{
112+
// Creates a projection stage to include only the "title" and
113+
// "course_id" fields
101114
// begin aggregate projection
102115
projectStage := bson.D{{"$project", bson.D{{"title", 1}, {"course_id", 1}}}}
103116

117+
// Aggregates results and prints them as structs including
118+
// only the specified fields
104119
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{projectStage})
105120
if err != nil {
106121
panic(err)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Creates filters and retrieves documents that match the filters
12
package main
23

34
import (
@@ -62,8 +63,10 @@ func main() {
6263

6364
fmt.Println("\nLiteral Value:\n")
6465
{
66+
// Creates a filter to match documents that have a "type" value of "Oolong"
6567
filter := bson.D{{"type", "Oolong"}}
6668

69+
// Retrieves documents that match the filter and prints them as structs
6770
cursor, err := coll.Find(context.TODO(), filter)
6871
if err != nil {
6972
panic(err)
@@ -81,8 +84,10 @@ func main() {
8184

8285
fmt.Println("\nComparison:\n")
8386
{
87+
// Creates a filter to match documents that have a "rating" value below 7
8488
filter := bson.D{{"rating", bson.D{{"$lt", 7}}}}
8589

90+
// Retrieves documents that match the filter and prints them as structs
8691
cursor, err := coll.Find(context.TODO(), filter)
8792
if err != nil {
8893
panic(err)
@@ -100,6 +105,7 @@ func main() {
100105

101106
fmt.Println("\nLogical:\n")
102107
{
108+
// Creates a filter to match documents that have a "rating" value less than or equal to 10 and greater than 7
103109
filter := bson.D{
104110
{"$and",
105111
bson.A{
@@ -109,6 +115,7 @@ func main() {
109115
},
110116
}
111117

118+
// Retrieves documents that match the filter and prints them as structs
112119
cursor, err := coll.Find(context.TODO(), filter)
113120
if err != nil {
114121
panic(err)
@@ -126,8 +133,10 @@ func main() {
126133

127134
fmt.Println("\nElement:\n")
128135
{
136+
// Creates a filter to match documents that do not contain the "vendor" field
129137
filter := bson.D{{"vendor", bson.D{{"$exists", false}}}}
130138

139+
// Retrieves documents that match the filter and prints them as structs
131140
cursor, err := coll.Find(context.TODO(), filter)
132141
if err != nil {
133142
panic(err)
@@ -145,8 +154,10 @@ func main() {
145154

146155
fmt.Println("\nEvaluation:\n")
147156
{
157+
// Creates a filter to match documents that have a "type" value starting with the letter "E"
148158
filter := bson.D{{"type", bson.D{{"$regex", "^E"}}}}
149159

160+
// Retrieves documents that match the filter and prints them as structs
150161
cursor, err := coll.Find(context.TODO(), filter)
151162
if err != nil {
152163
panic(err)
@@ -164,8 +175,10 @@ func main() {
164175

165176
fmt.Println("\nArray:\n")
166177
{
178+
// Creates a filter to match documents where the "vendor" array contains "C"
167179
filter := bson.D{{"vendor", bson.D{{"$all", bson.A{"C"}}}}}
168180

181+
// Retrieves documents that match the filter and prints them as structs
169182
cursor, err := coll.Find(context.TODO(), filter)
170183
if err != nil {
171184
panic(err)
@@ -183,8 +196,10 @@ func main() {
183196

184197
fmt.Println("\nBitwise:\n")
185198
{
199+
// Creates a filter to match documents where the "rating" value has the same bits set as 6
186200
filter := bson.D{{"rating", bson.D{{"$bitsAllSet", 6}}}}
187201

202+
// Retrieves documents that match the filter and prints them as structs
188203
cursor, err := coll.Find(context.TODO(), filter)
189204
if err != nil {
190205
panic(err)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Retrieves documents that match specified filters
12
package main
23

34
import (
@@ -61,6 +62,7 @@ func main() {
6162

6263
fmt.Println("\nFind:\n")
6364
{
65+
// Creates a filter to match documents that have a "rating" value between 5 and 9
6466
// begin find docs
6567
filter := bson.D{
6668
{"$and",
@@ -72,6 +74,7 @@ func main() {
7274
sort := bson.D{{"date_ordered", 1}}
7375
opts := options.Find().SetSort(sort)
7476

77+
// Retrieves documents that match the filter and prints them as structs
7578
cursor, err := coll.Find(context.TODO(), filter, opts)
7679
if err != nil {
7780
panic(err)
@@ -90,10 +93,14 @@ func main() {
9093

9194
fmt.Println("\nFind One:\n")
9295
{
96+
// Creates a filter to match documents that have a
97+
// "date_ordered" value before December 2009
9398
// begin find one docs
9499
filter := bson.D{{"date_ordered", bson.D{{"$lte", time.Date(2009, 11, 30, 0, 0, 0, 0, time.Local)}}}}
95100
opts := options.FindOne().SetSkip(2)
96101

102+
// Retrieves a document that matches the filter and prints it as
103+
// a struct
97104
var result Review
98105
err := coll.FindOne(context.TODO(), filter, opts).Decode(&result)
99106
if err != nil {
@@ -113,9 +120,13 @@ func main() {
113120
panic(err)
114121
}
115122

123+
// Creates a filter to match a document that has the specified
124+
// "_id" value
116125
filter := bson.D{{"_id", id}}
117126
opts := options.FindOne().SetProjection(bson.D{{"item", 1}, {"rating", 1}})
118127

128+
// Retrieves a document that matches the filter and prints it as
129+
// a struct
119130
var result Review
120131
err = coll.FindOne(context.TODO(), filter, opts).Decode(&result)
121132
if err != nil {
@@ -129,6 +140,8 @@ func main() {
129140

130141
fmt.Println("\nAggregation:\n")
131142
{
143+
// Creates an aggregation to group documents by "item" and finds
144+
// the average "rating" value
132145
// begin aggregate docs
133146
groupStage := bson.D{
134147
{"$group", bson.D{
@@ -143,6 +156,7 @@ func main() {
143156
panic(err)
144157
}
145158

159+
// Prints the average "rating" for each item
146160
var results []bson.M
147161
if err = cursor.All(context.TODO(), &results); err != nil {
148162
panic(err)

source/index.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{+driver-long+}
33
=================
44

5-
.. default-domain:: mongodb
6-
75
.. facet::
86
:name: genre
97
:values: reference
@@ -35,11 +33,11 @@ You can add the driver to your application to work with MongoDB in Go.
3533
Download it using `go get <https://pkg.go.dev/cmd/go/internal/get>`__
3634
or set up a runnable project by following our Quick Start guide.
3735

38-
Compatibility
39-
-------------
36+
Connect to a Compatible MongoDB Deployment
37+
------------------------------------------
4038

41-
You can use the {+driver-short+} to connect to deployments hosted in the
42-
following environments:
39+
You can use the {+driver-short+} to connect to MongoDB
40+
deployments running on one of the following hosted services or editions:
4341

4442
.. include:: /includes/fact-environments.rst
4543

0 commit comments

Comments
 (0)