diff --git a/source/crud/delete.txt b/source/crud/delete.txt index aeaecefd..890b7a30 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -136,14 +136,54 @@ method: ``DeleteMany()``, the driver would delete the first of the two matched documents. -Additional Information ----------------------- +DeleteOne() Example: Full File +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. include:: /includes/usage-examples/example-intro.rst + +The following example is a fully runnable file that finds and deletes an +existing document in the ``restaurants`` collection. + +.. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/deleteOne.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Documents deleted: 1 + +DeleteMany() Example: Full File +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. include:: /includes/usage-examples/example-intro.rst + +The following example is a fully runnable file that performs the following +actions on the ``restaurants`` collection: -For runnable examples of the delete operations, see the following usage -examples: +- Matches and deletes documents where the ``cuisine`` field value is ``German`` + and the ``borough`` field value is ``Queens`` +- Deletes all matching documents -- :ref:`golang-delete-one` -- :ref:`golang-delete-many` +.. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/deleteMany.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Documents deleted: 6 + +Additional Information +---------------------- To learn more about performing the operations mentioned, see the following guides: @@ -160,7 +200,7 @@ API Documentation ~~~~~~~~~~~~~~~~~ To learn more about any of the methods or types discussed in this -guide, see the following API Documentation: +guide, see the following API documentation: - `DeleteOne() <{+api+}/mongo#Collection.DeleteOne>`__ - `DeleteMany() <{+api+}/mongo#Collection.DeleteMany>`__ diff --git a/source/includes/usage-examples/code-snippets/deleteMany.go b/source/includes/usage-examples/code-snippets/deleteMany.go index 0bcc60a3..a91f6965 100644 --- a/source/includes/usage-examples/code-snippets/deleteMany.go +++ b/source/includes/usage-examples/code-snippets/deleteMany.go @@ -13,6 +13,14 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) +// Defines a Restaurant struct as a model for documents in the "restaurants" collection +type Restaurant struct { + ID bson.ObjectID `bson:"_id"` + Name string `bson:"name"` + Borough string `bson:"borough"` + Cuisine string `bson:"cuisine"` +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -33,20 +41,21 @@ func main() { } }() - // begin deleteMany - coll := client.Database("sample_mflix").Collection("movies") - filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}} + coll := client.Database("sample_restaurants").Collection("restaurants") + filter := bson.D{ + {"borough", "Queens"}, + {"cuisine", "German"}, + } - // Deletes all documents that have a "runtime" value greater than 800 + // Deletes all documents that have a "Borough" value of "Queens" and a "Cuisine" value of "German" results, err := coll.DeleteMany(context.TODO(), filter) if err != nil { panic(err) } - // end deleteMany // Prints the number of deleted documents fmt.Printf("Documents deleted: %d\n", results.DeletedCount) - // When you run this file for the first time, it should print: - // Documents deleted: 4 + // When you run this file for the first time, it prints output similar to the following: + // Documents deleted: 6 } diff --git a/source/includes/usage-examples/code-snippets/deleteOne.go b/source/includes/usage-examples/code-snippets/deleteOne.go index 8d23b987..67779e52 100644 --- a/source/includes/usage-examples/code-snippets/deleteOne.go +++ b/source/includes/usage-examples/code-snippets/deleteOne.go @@ -13,6 +13,16 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" ) +// Defines a Restaurant struct as a model for documents in the "restaurants" collection +type Restaurant struct { + ID bson.ObjectID `bson:"_id"` + Name string `bson:"name"` + Cuisine string `bson:"cuisine,omitempty"` + Address interface{} `bson:"address,omitempty"` + Borough string `bson:"borough,omitempty"` + Grades []interface{} `bson:"grades,omitempty"` +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -33,22 +43,20 @@ func main() { } }() - // begin deleteOne - coll := client.Database("sample_mflix").Collection("movies") - filter := bson.D{{"title", "Twilight"}} + coll := client.Database("sample_restaurants").Collection("restaurants") + filter := bson.D{{"name", "New Corner"}} - // Deletes the first document that has a "title" value of "Twilight" + // Deletes the first document that has a "name" value of "New Corner" result, err := coll.DeleteOne(context.TODO(), filter) // Prints a message if any errors occur during the operation if err != nil { panic(err) } - // end deleteOne // Prints the number of deleted documents fmt.Printf("Documents deleted: %d\n", result.DeletedCount) - // When you run this file for the first time, it should print: + // When you run this file for the first time, it prints output similar to the following: // Documents deleted: 1 } 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