Skip to content

Commit

Permalink
DOCSP-34174: codewhisperer pt 9 (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Nov 30, 2023
1 parent a6135ff commit 984af5d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Performs compound operations to find and modify data by using the Go driver
package main

import (
Expand Down Expand Up @@ -55,50 +56,66 @@ func main() {

fmt.Println("\nFindOneAndDelete:\n")
{
// Creates a filter to match documents where the "enrollment"
// value is less than 20
//begin FindOneAndDelete
filter := bson.D{{"enrollment", bson.D{{"$lt", 20}}}}

// Finds and deletes the first matching document in one action
var deletedDoc Course
err := coll.FindOneAndDelete(context.TODO(), filter).Decode(&deletedDoc)
if err != nil {
panic(err)
}

// Prints the contents of the deleted document
res, _ := bson.MarshalExtJSON(deletedDoc, false, false)
fmt.Println(string(res))
//end FindOneAndDelete
}

fmt.Println("\nFindOneAndUpdate:\n")
{
// Creates a filter to match documents where the "title"
// value includes the string "Modern"
//begin FindOneAndUpdate
filter := bson.D{{"title", bson.D{{"$regex", "Modern"}}}}

// Creates instructions to set the "enrollment" field to 32
update := bson.D{{"$set", bson.D{{"enrollment", 32}}}}
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)

// Finds and updates the first matching document in one action
var updatedDoc Course
err := coll.FindOneAndUpdate(context.TODO(), filter, update, opts).Decode(&updatedDoc)
if err != nil {
panic(err)
}

// Prints the contents of the document after the update
res, _ := bson.MarshalExtJSON(updatedDoc, false, false)
fmt.Println(string(res))
//end FindOneAndUpdate
}

fmt.Println("\nFindOneAndReplace:\n")
{
// Creates a filter to match documents where the "title"
// value is "Representation Theory"
//begin FindOneAndReplace
filter := bson.D{{"title", "Representation Theory"}}

// Creates a new document to replace the matched document
replacement := Course{Title: "Combinatorial Theory", Enrollment: 35}

// Finds and replaces the first matching document in one action
var previousDoc Course
err := coll.FindOneAndReplace(context.TODO(), filter, replacement).Decode(&previousDoc)
if err != nil {
panic(err)
}

// Prints the contents of the document before the replacement
res, _ := bson.MarshalExtJSON(previousDoc, false, false)
fmt.Println(string(res))
//end FindOneAndReplace
Expand Down
8 changes: 7 additions & 1 deletion source/includes/usage-examples/code-snippets/updateOne.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Updates the first document that matches a query filter by using the Go driver
package main

import (
Expand Down Expand Up @@ -37,15 +38,20 @@ func main() {
coll := client.Database("sample_restaurants").Collection("restaurants")
id, _ := primitive.ObjectIDFromHex("5eb3d668b31de5d588f42a7a")
filter := bson.D{{"_id", id}}

// Creates instructions to add the "avg_rating" field to documents
update := bson.D{{"$set", bson.D{{"avg_rating", 4.4}}}}

// Updates the first document that has the specified "_id" value
result, err := coll.UpdateOne(context.TODO(), filter, update)
if err != nil {
panic(err)
}
// end updateone

// Prints the number of updated documents
fmt.Printf("Documents updated: %v\n", result.ModifiedCount)

// When you run this file for the first time, it should print:
// Number of documents replaced: 1
fmt.Printf("Documents updated: %v\n", result.ModifiedCount)
}
6 changes: 6 additions & 0 deletions source/includes/usage-examples/code-snippets/watch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Demonstrates how to open a change stream by using the Go driver
package main

import (
Expand Down Expand Up @@ -35,7 +36,11 @@ func main() {

// begin watch
coll := client.Database("sample_restaurants").Collection("restaurants")

// Creates instructions to watch for insert operations
pipeline := mongo.Pipeline{bson.D{{"$match", bson.D{{"operationType", "insert"}}}}}

// Creates a change stream that receives change events
cs, err := coll.Watch(context.TODO(), pipeline)
if err != nil {
panic(err)
Expand All @@ -44,6 +49,7 @@ func main() {

fmt.Println("Waiting For Change Events. Insert something in MongoDB!")

// Prints a message each time the change stream receives an event
for cs.Next(context.TODO()) {
var event bson.M
if err := cs.Decode(&event); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion source/usage-examples/changestream.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and prints inserted documents:
.. literalinclude:: /includes/usage-examples/code-snippets/watch.go
:start-after: begin watch
:end-before: end watch
:emphasize-lines: 3
:emphasize-lines: 7
:language: go
:dedent:

Expand Down
4 changes: 1 addition & 3 deletions source/usage-examples/updateOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
Update a Document
=================

.. default-domain:: mongodb

You can update a document in a collection by using the ``UpdateOne()``
method.

Expand All @@ -23,7 +21,7 @@ collection:
.. literalinclude:: /includes/usage-examples/code-snippets/updateOne.go
:start-after: begin updateone
:end-before: end updateone
:emphasize-lines: 6
:emphasize-lines: 9
:language: go
:dedent:

Expand Down

0 comments on commit 984af5d

Please sign in to comment.