Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 100 additions & 7 deletions source/crud/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,107 @@ 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. 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 a 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

.. 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

For runnable examples of the delete operations, see the following usage
examples:
DeleteMany() Example: Full File
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- :ref:`golang-delete-one`
- :ref:`golang-delete-many`
.. 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:

.. 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

.. input:: /includes/usage-examples/code-snippets/deleteManyBson.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:
Expand All @@ -160,7 +253,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>`__
Expand Down
29 changes: 22 additions & 7 deletions source/includes/usage-examples/code-snippets/deleteMany.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment here: https://github.com/mongodb/docs-golang/pull/541/files#r2229480471

Suggest retaining the bson.D filtering solution.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

Borough string `bson:"borough"`
Cuisine string `bson:"cuisine"`
}

func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
Expand All @@ -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
// 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
}
53 changes: 53 additions & 0 deletions source/includes/usage-examples/code-snippets/deleteManyBson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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
}
21 changes: 15 additions & 6 deletions source/includes/usage-examples/code-snippets/deleteOne.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -33,22 +44,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 := 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)

// 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
}
52 changes: 52 additions & 0 deletions source/includes/usage-examples/code-snippets/deleteOneBson.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading