From 276fe5ddc40f30a5d8ceef9c9950ba88263a5896 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Tue, 22 Jul 2025 15:32:28 -0700 Subject: [PATCH 1/8] update deleteOne usage example --- source/crud/delete.txt | 37 +++++++++++++ .../usage-examples/code-snippets/deleteOne.go | 19 +++++-- .../code-snippets/deleteOneBson.go | 52 +++++++++++++++++++ 3 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 source/includes/usage-examples/code-snippets/deleteOneBson.go diff --git a/source/crud/delete.txt b/source/crud/delete.txt index aeaecefd..1adbddb6 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -136,6 +136,43 @@ method: ``DeleteMany()``, the driver would delete the first of the two matched documents. +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. Select the +:guilabel:`Struct` or :guilabel:`bson.D` tba to see the corresponding code: + +.. tabs:: + + .. tab:: Struct + :tabid: structExample + + The following code uses structs to define and delete a document in the + ``restaurants`` collection: + + .. input:: /includes/usage-examples/code-snippets/deleteOne.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Documents deleted: 1 + + .. input:: /includes/usage-examples/code-snippets/deleteOneBson.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Documents deleted: 1 + Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/deleteOne.go b/source/includes/usage-examples/code-snippets/deleteOne.go index 8d23b987..f2e1afd0 100644 --- a/source/includes/usage-examples/code-snippets/deleteOne.go +++ b/source/includes/usage-examples/code-snippets/deleteOne.go @@ -13,6 +13,17 @@ 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"` +} + +// Creates a filter struct to specify the document to delete +type DeleteRestaurantFilter struct { + Name string `bson:"name"` +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -33,18 +44,16 @@ func main() { } }() - // begin deleteOne - coll := client.Database("sample_mflix").Collection("movies") - filter := bson.D{{"title", "Twilight"}} + coll := client.Database("sample_restaurants").Collection("restaurants") + filter := DeleteRestaurantFilter{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) diff --git a/source/includes/usage-examples/code-snippets/deleteOneBson.go b/source/includes/usage-examples/code-snippets/deleteOneBson.go new file mode 100644 index 00000000..db2b51a6 --- /dev/null +++ b/source/includes/usage-examples/code-snippets/deleteOneBson.go @@ -0,0 +1,52 @@ +// Deletes a document from a collection 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{{"name", "New Corner"}} + + // 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) + } + + // Prints the number of deleted documents + fmt.Printf("Documents deleted: %d\n", result.DeletedCount) + + // When you run this file for the first time, it prints output similar to the following: + // Documents deleted: 1 +} From a04ad3b5c9a5357375b63d3304eab0f3459f2d2f Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Tue, 22 Jul 2025 15:54:47 -0700 Subject: [PATCH 2/8] update deleteMany example --- source/crud/delete.txt | 45 ++++++++++++++- .../code-snippets/deleteMany.go | 27 +++++++-- .../code-snippets/deleteManyBson.go | 55 +++++++++++++++++++ 3 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 source/includes/usage-examples/code-snippets/deleteManyBson.go diff --git a/source/crud/delete.txt b/source/crud/delete.txt index 1adbddb6..f19fadfa 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -173,10 +173,49 @@ existing document in the ``restaurants`` collection. Select the Documents deleted: 1 -Additional Information ----------------------- +DeleteMany() Example: Full File +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. include:: /includes/usage-examples/example-intro.rst + +The following example is a fully runnable file that finds and deletes multiple +existing documents in the ``restaurants`` collection. Select the +:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: + +.. tabs:: + + .. tab:: Struct + :tabid: structExample + + The following code uses structs to define and delete documents in the + ``restaurants`` collection: + + .. input:: /includes/usage-examples/code-snippets/deleteMany.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Documents deleted: 2 + + .. tab:: BSON + :tabid: bsonExample + + The following code uses BSON documents to define and delete documents in the + ``restaurants`` collection: + + .. input:: /includes/usage-examples/code-snippets/deleteManyBson.go + :language: go + :dedent: + + .. output:: + :language: none + :visible: false + + Documents deleted: 2 -For runnable examples of the delete operations, see the following usage examples: - :ref:`golang-delete-one` diff --git a/source/includes/usage-examples/code-snippets/deleteMany.go b/source/includes/usage-examples/code-snippets/deleteMany.go index 0bcc60a3..24de2f04 100644 --- a/source/includes/usage-examples/code-snippets/deleteMany.go +++ b/source/includes/usage-examples/code-snippets/deleteMany.go @@ -13,6 +13,20 @@ 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"` +} + +// Creates a filter struct to specify the documents to delete +type DeleteRestaurantFilter struct { + Borough string `bson:"borough"` + Cuisine string `bson:"cuisine"` +} + func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -33,20 +47,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 := DeleteRestaurantFilter{ + Borough: "Queens", + Cuisine: "German", + } // Deletes all documents that have a "runtime" value greater than 800 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/deleteManyBson.go b/source/includes/usage-examples/code-snippets/deleteManyBson.go new file mode 100644 index 00000000..2b05d11f --- /dev/null +++ b/source/includes/usage-examples/code-snippets/deleteManyBson.go @@ -0,0 +1,55 @@ +// Deletes multiple documents from a collection 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) + } + }() + + // begin deleteMany + 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 + 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 prints output similar to the following: + // Documents deleted: 6 +} From f34e8bdb0d4e95cf8799ffa18ce352933fd2a365 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Tue, 22 Jul 2025 15:55:35 -0700 Subject: [PATCH 3/8] update intro --- source/crud/delete.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/crud/delete.txt b/source/crud/delete.txt index f19fadfa..1ef266d7 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -200,10 +200,10 @@ existing documents in the ``restaurants`` collection. Select the Documents deleted: 2 - .. tab:: BSON + .. tab:: bson.D :tabid: bsonExample - The following code uses BSON documents to define and delete documents in the + The following code uses a bson.D type to define and delete documents in the ``restaurants`` collection: .. input:: /includes/usage-examples/code-snippets/deleteManyBson.go From fbe3d9ffbe4c010c2fe7ed417c30b715d2009525 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Tue, 22 Jul 2025 16:10:55 -0700 Subject: [PATCH 4/8] remove links to usage examples and fix formatting --- source/crud/delete.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/source/crud/delete.txt b/source/crud/delete.txt index 1ef266d7..372609c2 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -163,6 +163,12 @@ existing document in the ``restaurants`` collection. Select the Documents deleted: 1 + .. tab:: bson.D + :tabid: bsonExample + + The following code uses a bson.D type to define and delete a document in the + ``restaurants`` collection: + .. input:: /includes/usage-examples/code-snippets/deleteOneBson.go :language: go :dedent: @@ -216,10 +222,8 @@ existing documents in the ``restaurants`` collection. Select the Documents deleted: 2 -examples: - -- :ref:`golang-delete-one` -- :ref:`golang-delete-many` +Additional Information +---------------------- To learn more about performing the operations mentioned, see the following guides: @@ -236,7 +240,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>`__ From 97e567fc6db0bc9c4b5313050c07e0fff9b94226 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Tue, 22 Jul 2025 16:27:01 -0700 Subject: [PATCH 5/8] fix formatting --- source/crud/delete.txt | 69 +++++++++++-------- .../usage-examples/code-snippets/deleteOne.go | 2 +- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/source/crud/delete.txt b/source/crud/delete.txt index 372609c2..5ef9b00f 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -153,15 +153,18 @@ existing document in the ``restaurants`` collection. Select the The following code uses structs to define and delete a document in the ``restaurants`` collection: - .. input:: /includes/usage-examples/code-snippets/deleteOne.go - :language: go - :dedent: + .. io-code-block:: + :copyable: true - .. output:: - :language: none - :visible: false + .. input:: /includes/usage-examples/code-snippets/deleteOne.go + :language: go + :dedent: - Documents deleted: 1 + .. output:: + :language: none + :visible: false + + Documents deleted: 1 .. tab:: bson.D :tabid: bsonExample @@ -169,15 +172,18 @@ existing document in the ``restaurants`` collection. Select the The following code uses a bson.D type to define and delete a document in the ``restaurants`` collection: - .. input:: /includes/usage-examples/code-snippets/deleteOneBson.go - :language: go - :dedent: + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/deleteOneBson.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Documents deleted: 1 + Documents deleted: 1 DeleteMany() Example: Full File ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -196,15 +202,19 @@ existing documents in the ``restaurants`` collection. Select the The following code uses structs to define and delete documents in the ``restaurants`` collection: - .. input:: /includes/usage-examples/code-snippets/deleteMany.go - :language: go - :dedent: + .. io-code-block:: + :copyable: true - .. output:: - :language: none - :visible: false + .. input:: /includes/usage-examples/code-snippets/deleteMany.go + :language: go + :dedent: + - Documents deleted: 2 + .. output:: + :language: none + :visible: false + + Documents deleted: 6 .. tab:: bson.D :tabid: bsonExample @@ -212,15 +222,18 @@ existing documents in the ``restaurants`` collection. Select the The following code uses a bson.D type to define and delete documents in the ``restaurants`` collection: - .. input:: /includes/usage-examples/code-snippets/deleteManyBson.go - :language: go - :dedent: + .. io-code-block:: + :copyable: true + + .. input:: /includes/usage-examples/code-snippets/deleteManyBson.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Documents deleted: 2 + Documents deleted: 6 Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/deleteOne.go b/source/includes/usage-examples/code-snippets/deleteOne.go index f2e1afd0..49fa1524 100644 --- a/source/includes/usage-examples/code-snippets/deleteOne.go +++ b/source/includes/usage-examples/code-snippets/deleteOne.go @@ -58,6 +58,6 @@ func main() { // 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 } From 4d127ed11dac364faf5db421781c3b98a65c6812 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Wed, 23 Jul 2025 09:34:38 -0700 Subject: [PATCH 6/8] update code comments --- source/includes/usage-examples/code-snippets/deleteMany.go | 2 +- .../includes/usage-examples/code-snippets/deleteManyBson.go | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/source/includes/usage-examples/code-snippets/deleteMany.go b/source/includes/usage-examples/code-snippets/deleteMany.go index 24de2f04..00c913f6 100644 --- a/source/includes/usage-examples/code-snippets/deleteMany.go +++ b/source/includes/usage-examples/code-snippets/deleteMany.go @@ -53,7 +53,7 @@ func main() { 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) diff --git a/source/includes/usage-examples/code-snippets/deleteManyBson.go b/source/includes/usage-examples/code-snippets/deleteManyBson.go index 2b05d11f..66bcc53a 100644 --- a/source/includes/usage-examples/code-snippets/deleteManyBson.go +++ b/source/includes/usage-examples/code-snippets/deleteManyBson.go @@ -33,19 +33,17 @@ func main() { } }() - // begin deleteMany 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) From 8d7212ead46f074738598b74066e23144ee29ac5 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Thu, 24 Jul 2025 10:09:39 -0700 Subject: [PATCH 7/8] js feedback --- source/crud/delete.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/crud/delete.txt b/source/crud/delete.txt index 5ef9b00f..edb5eb99 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -143,7 +143,7 @@ DeleteOne() Example: Full File The following example is a fully runnable file that finds and deletes an existing document in the ``restaurants`` collection. Select the -:guilabel:`Struct` or :guilabel:`bson.D` tba to see the corresponding code: +:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: .. tabs:: From c81b8ff5a33593bdd3c86329e9df827f94af2bb9 Mon Sep 17 00:00:00 2001 From: Stephanie Aurelio Date: Mon, 28 Jul 2025 10:02:25 -0700 Subject: [PATCH 8/8] tech feedback --- source/crud/delete.txt | 85 ++++--------------- .../code-snippets/deleteMany.go | 12 +-- .../code-snippets/deleteManyBson.go | 53 ------------ .../usage-examples/code-snippets/deleteOne.go | 15 ++-- .../code-snippets/deleteOneBson.go | 52 ------------ .../code-snippets/insertOneBsonD.go | 0 6 files changed, 26 insertions(+), 191 deletions(-) delete mode 100644 source/includes/usage-examples/code-snippets/deleteManyBson.go delete mode 100644 source/includes/usage-examples/code-snippets/deleteOneBson.go delete mode 100644 source/includes/usage-examples/code-snippets/insertOneBsonD.go diff --git a/source/crud/delete.txt b/source/crud/delete.txt index edb5eb99..890b7a30 100644 --- a/source/crud/delete.txt +++ b/source/crud/delete.txt @@ -142,18 +142,9 @@ 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. Select the -:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code: +existing document in the ``restaurants`` collection. -.. tabs:: - - .. tab:: Struct - :tabid: structExample - - The following code uses structs to define and delete a document in the - ``restaurants`` collection: - - .. io-code-block:: +.. io-code-block:: :copyable: true .. input:: /includes/usage-examples/code-snippets/deleteOne.go @@ -166,74 +157,30 @@ existing document in the ``restaurants`` collection. Select the Documents deleted: 1 - .. tab:: bson.D - :tabid: bsonExample - - The following code uses a bson.D type to define and delete a document in the - ``restaurants`` collection: - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/deleteOneBson.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 finds and deletes 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:: +- Matches and deletes documents where the ``cuisine`` field value is ``German`` + and the ``borough`` field value is ``Queens`` +- Deletes all matching documents - .. tab:: Struct - :tabid: structExample - - The following code uses structs to define and delete documents in the - ``restaurants`` collection: - - .. io-code-block:: - :copyable: true - - .. input:: /includes/usage-examples/code-snippets/deleteMany.go - :language: go - :dedent: - - - .. output:: - :language: none - :visible: false - - Documents deleted: 6 - - .. tab:: bson.D - :tabid: bsonExample - - The following code uses a bson.D type to define and delete documents in the - ``restaurants`` collection: - - .. io-code-block:: - :copyable: true +.. io-code-block:: + :copyable: true - .. input:: /includes/usage-examples/code-snippets/deleteManyBson.go - :language: go - :dedent: + .. input:: /includes/usage-examples/code-snippets/deleteMany.go + :language: go + :dedent: - .. output:: - :language: none - :visible: false + .. output:: + :language: none + :visible: false - Documents deleted: 6 + Documents deleted: 6 Additional Information ---------------------- diff --git a/source/includes/usage-examples/code-snippets/deleteMany.go b/source/includes/usage-examples/code-snippets/deleteMany.go index 00c913f6..a91f6965 100644 --- a/source/includes/usage-examples/code-snippets/deleteMany.go +++ b/source/includes/usage-examples/code-snippets/deleteMany.go @@ -21,12 +21,6 @@ type Restaurant struct { Cuisine string `bson:"cuisine"` } -// Creates a filter struct to specify the documents to delete -type DeleteRestaurantFilter struct { - Borough string `bson:"borough"` - Cuisine string `bson:"cuisine"` -} - func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") @@ -48,9 +42,9 @@ func main() { }() coll := client.Database("sample_restaurants").Collection("restaurants") - filter := DeleteRestaurantFilter{ - Borough: "Queens", - Cuisine: "German", + filter := bson.D{ + {"borough", "Queens"}, + {"cuisine", "German"}, } // Deletes all documents that have a "Borough" value of "Queens" and a "Cuisine" value of "German" diff --git a/source/includes/usage-examples/code-snippets/deleteManyBson.go b/source/includes/usage-examples/code-snippets/deleteManyBson.go deleted file mode 100644 index 66bcc53a..00000000 --- a/source/includes/usage-examples/code-snippets/deleteManyBson.go +++ /dev/null @@ -1,53 +0,0 @@ -// Deletes multiple documents from a collection 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{ - {"borough", "Queens"}, - {"cuisine", "German"}, - } - - // 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) - } - - // Prints the number of deleted documents - fmt.Printf("Documents deleted: %d\n", results.DeletedCount) - - // 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 49fa1524..67779e52 100644 --- a/source/includes/usage-examples/code-snippets/deleteOne.go +++ b/source/includes/usage-examples/code-snippets/deleteOne.go @@ -15,13 +15,12 @@ import ( // 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"` -} - -// Creates a filter struct to specify the document to delete -type DeleteRestaurantFilter struct { - Name string `bson:"name"` + 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() { @@ -45,7 +44,7 @@ func main() { }() coll := client.Database("sample_restaurants").Collection("restaurants") - filter := DeleteRestaurantFilter{Name: "New Corner"} + filter := bson.D{{"name", "New Corner"}} // Deletes the first document that has a "name" value of "New Corner" result, err := coll.DeleteOne(context.TODO(), filter) diff --git a/source/includes/usage-examples/code-snippets/deleteOneBson.go b/source/includes/usage-examples/code-snippets/deleteOneBson.go deleted file mode 100644 index db2b51a6..00000000 --- a/source/includes/usage-examples/code-snippets/deleteOneBson.go +++ /dev/null @@ -1,52 +0,0 @@ -// Deletes a document from a collection 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{{"name", "New Corner"}} - - // 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) - } - - // Prints the number of deleted documents - fmt.Printf("Documents deleted: %d\n", result.DeletedCount) - - // 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