diff --git a/source/crud/update.txt b/source/crud/update.txt index c3c8660c..2bdc98dc 100644 --- a/source/crud/update.txt +++ b/source/crud/update.txt @@ -209,94 +209,49 @@ UpdateOne() Example: Full File .. include:: /includes/usage-examples/example-intro.rst -The following example is a fully runnable file that finds and updates an -existing document in the ``restaurants`` collection. Select the -:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: +The following example is a fully runnable file that performs the following +actions on the ``restaurants`` collection: -.. tabs:: +- Finds a document with a specific ``_id`` +- Updates the ``avg_rating`` field value of the matched document - .. tab:: Struct - :tabid: structExample - - The following code uses a struct to define the filter and update a document in the ``restuarants`` collection: - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/updateOne.go - :language: go - :dedent: - - .. output:: - :language: none - :visible: false - - Documents updated: 1 - - .. tab:: bson.D - :tabid: bsonDExample - - The following code uses a ``bson.D`` type to define the filter and update a document in the ``restuarants`` collection: - - .. io-code-block:: - :copyable: true +.. io-code-block:: + :copyable: true - .. input:: /includes/usage-examples/code-snippets/updateOneBson.go - :language: go - :dedent: + .. input:: /includes/usage-examples/code-snippets/updateOne.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Documents updated: 1 + Documents updated: 1 UpdateMany() Example: Full File ------------------------------- .. include:: /includes/usage-examples/example-intro.rst -The following example is a fully runnable file that finds and updates multiple -existing documents in the ``restaurants`` collection. Select the -:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: +The following example is a fully runnable file that performs the following +actions on the ``restaurants`` collection: -.. tabs:: +- Finds documents with the ``cuisine`` field value of ``"Pizza"`` and the + ``borough`` field value of ``"Brooklyn"`` +- Updates the ``avg_rating`` field value of the matched documents - .. tab:: Struct - :tabid: structExample - - The following code uses a struct to define the filter and update multiple documents in the ``restuarants`` collection: - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/updateMany.go - :language: go - :dedent: - - .. output:: - :language: none - :visible: false - - Documents updated: 296 - - .. tab:: bson.D - :tabid: bsonDExample - - The following code uses a ``bson.D`` type to define the filter and update multiple documents in the ``restuarants`` collection: - - .. io-code-block:: - :copyable: true +.. io-code-block:: + :copyable: true - .. input:: /includes/usage-examples/code-snippets/updateManyBson.go - :language: go - :dedent: + .. input:: /includes/usage-examples/code-snippets/updateMany.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Documents updated: 296 + Documents updated: 296 Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/insertOneBsonD.go b/source/includes/usage-examples/code-snippets/insertOneBsonD.go deleted file mode 100644 index e69de29b..00000000 diff --git a/source/includes/usage-examples/code-snippets/updateMany.go b/source/includes/usage-examples/code-snippets/updateMany.go index 9577c3ef..2731812e 100644 --- a/source/includes/usage-examples/code-snippets/updateMany.go +++ b/source/includes/usage-examples/code-snippets/updateMany.go @@ -21,17 +21,6 @@ type Restaurant struct { AverageRating float64 `bson:"avg_rating,omitempty"` } -// Create a filter struct to specify the documents to update -type UpdateManyRestaurantFilter struct { - Cuisine string `bson:"cuisine"` - Borough string `bson:"borough"` -} - -// Defines a RestaurantUpdate struct to specify the fields to update -type RestaurantUpdateMany struct { - AverageRating float64 `bson:"avg_rating"` -} - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -53,13 +42,10 @@ func main() { }() coll := client.Database("sample_restaurants").Collection("restaurants") - filter := UpdateManyRestaurantFilter{ - Cuisine: "Pizza", - Borough: "Brooklyn", - } + filter := bson.D{{"cuisine", "Pizza"}, {"borough", "Brooklyn"}} - // Creates instructions to update the values of the "AverageRating" field - update := bson.D{{"$set", RestaurantUpdateMany{AverageRating: 4.5}}} + // Creates instructions to update the values of the "avg_rating" field + update := bson.D{{"$set", bson.D{{"avg_rating", 4.5}}}} // Updates documents in which the value of the "Cuisine" // field is "Pizza" diff --git a/source/includes/usage-examples/code-snippets/updateManyBson.go b/source/includes/usage-examples/code-snippets/updateManyBson.go deleted file mode 100644 index f4f2178a..00000000 --- a/source/includes/usage-examples/code-snippets/updateManyBson.go +++ /dev/null @@ -1,54 +0,0 @@ -// Updates documents that match a query filter by using the Go driver -package main - -import ( - "context" - "fmt" - "log" - "os" - - "github.com/joho/godotenv" - "go.mongodb.org/mongo-driver/v2/bson" - "go.mongodb.org/mongo-driver/v2/mongo" - "go.mongodb.org/mongo-driver/v2/mongo/options" -) - -func main() { - if err := godotenv.Load(); err != nil { - log.Println("No .env file found") - } - - var uri string - if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") - } - - client, err := mongo.Connect(options.Client().ApplyURI(uri)) - if err != nil { - panic(err) - } - defer func() { - if err = client.Disconnect(context.TODO()); err != nil { - panic(err) - } - }() - - coll := client.Database("sample_restaurants").Collection("restaurants") - filter := bson.D{{"cuisine", "Pizza"}, {"borough", "Brooklyn"}} - - // Creates instructions to update the values of the "avg_rating" field - update := bson.D{{"$set", bson.D{{"avg_rating", 4.5}}}} - - // Updates documents in which the value of the "cuisine" field is "Pizza" - // and the value of the "borough" field is "Brooklyn" - result, err := coll.UpdateMany(context.TODO(), filter, update) - if err != nil { - panic(err) - } - - // 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 output similar to the following: - // Documents updated: 296 -} diff --git a/source/includes/usage-examples/code-snippets/updateOne.go b/source/includes/usage-examples/code-snippets/updateOne.go index 83edc509..455098f0 100644 --- a/source/includes/usage-examples/code-snippets/updateOne.go +++ b/source/includes/usage-examples/code-snippets/updateOne.go @@ -20,16 +20,6 @@ type Restaurant struct { AverageRating float64 `bson:"avg_rating,omitempty"` } -// Create a filter struct to specify the document to update -type UpdateRestaurantFilter struct { - ID bson.ObjectID `bson:"_id"` -} - -// Defines a RestaurantUpdate struct to specify the fields to update -type RestaurantUpdate struct { - AverageRating float64 `bson:"avg_rating"` -} - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -53,10 +43,10 @@ func main() { coll := client.Database("sample_restaurants").Collection("restaurants") id, _ := bson.ObjectIDFromHex("5eb3d668b31de5d588f4292b") - filter := UpdateRestaurantFilter{ID: id} + filter := bson.D{{"_id", id}} // Creates instructions to add the "avg_rating" field to documents - update := bson.D{{"$set", RestaurantUpdate{AverageRating: 4.4}}} + 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) diff --git a/source/includes/usage-examples/code-snippets/updateOneBson.go b/source/includes/usage-examples/code-snippets/updateOneBson.go deleted file mode 100644 index 250ab3c2..00000000 --- a/source/includes/usage-examples/code-snippets/updateOneBson.go +++ /dev/null @@ -1,54 +0,0 @@ -// Updates the first document that matches a query filter by using the Go driver -package main - -import ( - "context" - "fmt" - "log" - "os" - - "github.com/joho/godotenv" - "go.mongodb.org/mongo-driver/v2/bson" - "go.mongodb.org/mongo-driver/v2/mongo" - "go.mongodb.org/mongo-driver/v2/mongo/options" -) - -func main() { - if err := godotenv.Load(); err != nil { - log.Println("No .env file found") - } - - var uri string - if uri = os.Getenv("MONGODB_URI"); uri == "" { - log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") - } - - client, err := mongo.Connect(options.Client().ApplyURI(uri)) - if err != nil { - panic(err) - } - defer func() { - if err = client.Disconnect(context.TODO()); err != nil { - panic(err) - } - }() - - coll := client.Database("sample_restaurants").Collection("restaurants") - id, _ := bson.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) - } - - // 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 output similar to the following: - // Documents updated: 1 -}