-
Notifications
You must be signed in to change notification settings - Fork 33
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 5 #324
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,5 @@ | ||||||
// Retrieves documents that match the filter and applies a | ||||||
// sort to the results | ||||||
package main | ||||||
|
||||||
import ( | ||||||
|
@@ -56,6 +58,8 @@ func main() { | |||||
|
||||||
fmt.Println("\nAscending Sort:\n") | ||||||
{ | ||||||
// Retrieves matching documents and sets an ascending sort on | ||||||
// "enrollment" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S:
Suggested change
|
||||||
//begin ascending sort | ||||||
filter := bson.D{} | ||||||
opts := options.Find().SetSort(bson.D{{"enrollment", 1}}) | ||||||
|
@@ -66,6 +70,8 @@ func main() { | |||||
if err = cursor.All(context.TODO(), &results); err != nil { | ||||||
panic(err) | ||||||
} | ||||||
|
||||||
// Prints matched documents as structs | ||||||
for _, result := range results { | ||||||
res, _ := json.Marshal(result) | ||||||
fmt.Println(string(res)) | ||||||
|
@@ -75,6 +81,8 @@ func main() { | |||||
|
||||||
fmt.Println("\nDescending Sort:\n") | ||||||
{ | ||||||
// Retrieves matching documents and sets a descending sort on | ||||||
// "enrollment" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S:
Suggested change
|
||||||
//begin descending sort | ||||||
filter := bson.D{} | ||||||
opts := options.Find().SetSort(bson.D{{"enrollment", -1}}) | ||||||
|
@@ -85,6 +93,8 @@ func main() { | |||||
if err = cursor.All(context.TODO(), &results); err != nil { | ||||||
panic(err) | ||||||
} | ||||||
|
||||||
// Prints matched documents as structs | ||||||
for _, result := range results { | ||||||
res, _ := json.Marshal(result) | ||||||
fmt.Println(string(res)) | ||||||
|
@@ -94,6 +104,8 @@ func main() { | |||||
|
||||||
fmt.Println("\nMulti Sort:\n") | ||||||
{ | ||||||
// Retrieves matching documents and sets a descending sort on | ||||||
// "enrollment" and an ascending sort on "title" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
//begin multi sort | ||||||
filter := bson.D{} | ||||||
opts := options.Find().SetSort(bson.D{{"enrollment", -1}, {"title", 1}}) | ||||||
|
@@ -104,6 +116,8 @@ func main() { | |||||
if err = cursor.All(context.TODO(), &results); err != nil { | ||||||
panic(err) | ||||||
} | ||||||
|
||||||
// Prints matched documents as structs | ||||||
for _, result := range results { | ||||||
res, _ := json.Marshal(result) | ||||||
fmt.Println(string(res)) | ||||||
|
@@ -113,6 +127,8 @@ func main() { | |||||
|
||||||
fmt.Println("\nAggregation Sort:\n") | ||||||
{ | ||||||
// Uses an aggregation pipeline to set a descending sort on | ||||||
// "enrollment" and an ascending sort on "title" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: same as above (line 108) |
||||||
// begin aggregate sort | ||||||
sortStage := bson.D{{"$sort", bson.D{{"enrollment", -1}, {"title", 1}}}} | ||||||
|
||||||
|
@@ -125,6 +141,8 @@ func main() { | |||||
if err = cursor.All(context.TODO(), &results); err != nil { | ||||||
panic(err) | ||||||
} | ||||||
|
||||||
// Prints matched documents as structs | ||||||
for _, result := range results { | ||||||
res, _ := json.Marshal(result) | ||||||
fmt.Println(string(res)) | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,4 @@ | ||||||
// Updates array fields of documents by using the Go driver | ||||||
package main | ||||||
|
||||||
import ( | ||||||
|
@@ -54,12 +55,16 @@ func main() { | |||||
|
||||||
fmt.Println("\nPositional $ Operator:\n") | ||||||
{ | ||||||
// Creates a filter and update document to match a "sizes" array | ||||||
// entry and decrease the value by 2 | ||||||
// begin positional | ||||||
filter := bson.D{{"sizes", bson.D{{"$lte", 16}}}} | ||||||
update := bson.D{{"$inc", bson.D{{"sizes.$", -2}}}} | ||||||
opts := options.FindOneAndUpdate(). | ||||||
SetReturnDocument(options.After) | ||||||
|
||||||
// Updates the first document that matches the filter and prints | ||||||
// the updated document as a struct | ||||||
var updatedDoc Drink | ||||||
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc) | ||||||
if err != nil { | ||||||
|
@@ -86,13 +91,17 @@ func main() { | |||||
|
||||||
fmt.Println("\nPositional $[<identifier>] Operator:\n") | ||||||
{ | ||||||
// Creates a filter and update document to match "sizes" array | ||||||
// entries and remove those values | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: just to make it a little clearer
Suggested change
|
||||||
// begin filtered positional | ||||||
identifier := []interface{}{bson.D{{"hotOptions", bson.D{{"$regex", "hot"}}}}} | ||||||
update := bson.D{{"$unset", bson.D{{"styles.$[hotOptions]", ""}}}} | ||||||
opts := options.FindOneAndUpdate(). | ||||||
SetArrayFilters(options.ArrayFilters{Filters: identifier}). | ||||||
SetReturnDocument(options.After) | ||||||
|
||||||
// Updates the first document that matches the filter and prints | ||||||
// the updated document as a struct | ||||||
var updatedDoc Drink | ||||||
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc) | ||||||
if err != nil { | ||||||
|
@@ -118,11 +127,15 @@ func main() { | |||||
|
||||||
fmt.Println("\nPositional $[] Operator:\n") | ||||||
{ | ||||||
// Creates a filter and update document to match all "sizes" array | ||||||
// entries and multiply them by a value | ||||||
// begin positional all | ||||||
update := bson.D{{"$mul", bson.D{{"sizes.$[]", 29.57}}}} | ||||||
opts := options.FindOneAndUpdate(). | ||||||
SetReturnDocument(options.After) | ||||||
|
||||||
// Updates the first document that matches the filter and prints | ||||||
// the updated document as a struct | ||||||
var updatedDoc Drink | ||||||
err := coll.FindOneAndUpdate(context.TODO(), bson.D{}, update, opts).Decode(&updatedDoc) | ||||||
if err != nil { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.