Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-34174: codewhisperer pt 1 #320

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/limit.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Limits the number of documents returned by a query
package main

import (
Expand Down Expand Up @@ -56,10 +57,15 @@ func main() {

fmt.Println("\nLimit:\n")
{
// Creates a filter to match documents that have an
// "enrollment" value greater than 20
//begin limit
filter := bson.D{{"enrollment", bson.D{{"$gt", 20}}}}

// Sets a limit to return the first 2 matched documents
opts := options.Find().SetLimit(2)

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

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

fmt.Println("\nLimit, Skip, and Sort:\n")
{
// Creates an empty filter to match all documents
//begin multi options
filter := bson.D{}

// Sets options to sort by descending order on "enrollment",
// return only 2 documents, and skip the 1 matched document
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S:

Suggested change
// return only 2 documents, and skip the 1 matched document
// return only 2 documents, and skip the first matched document

opts := options.Find().SetSort(bson.D{{"enrollment", -1}}).SetLimit(2).SetSkip(1)

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

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

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

// Aggregates results and prints them as structs
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{limitStage})
if err != nil {
panic(err)
Expand Down
15 changes: 15 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/projection.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Specifies which document fields to return
package main

import (
Expand Down Expand Up @@ -54,10 +55,15 @@ func main() {

fmt.Println("\nExclude Projection:\n")
{
// Creates an empty filter to match all documents
//begin exclude projection
filter := bson.D{}

// Sets a projection to exclude the "course_id" and "enrollment" fields
opts := options.Find().SetProjection(bson.D{{"course_id", 0}, {"enrollment", 0}})

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

fmt.Println("\nInclude Projection:\n")
{
// Creates an empty filter to match all documents
//begin include projection
filter := bson.D{}

// Sets a projection to include the "title" and "enrollment" fields
opts := options.Find().SetProjection(bson.D{{"title", 1}, {"enrollment", 1}})

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

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

// Aggregates results and prints them as structs with the
// specified projection
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: for consistency, maybe change this to "...prints them as structs including only the specified fields"

cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{projectStage})
if err != nil {
panic(err)
Expand Down
15 changes: 15 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/query.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Creates filters and retrieves documents that match the filters
package main

import (
Expand Down Expand Up @@ -62,8 +63,10 @@ func main() {

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

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

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

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

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

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

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

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

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

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

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

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

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

// Retrieves documents that match the filter and prints them as structs
cursor, err := coll.Find(context.TODO(), filter)
if err != nil {
panic(err)
Expand Down
14 changes: 14 additions & 0 deletions source/includes/fundamentals/code-snippets/CRUD/retrieve.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Retrieves documents that match specified filters
package main

import (
Expand Down Expand Up @@ -61,6 +62,7 @@ func main() {

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

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

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

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

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

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

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

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