Skip to content

DOCSP-51819 Standardize and move delete usage examples #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 47 additions & 7 deletions source/crud/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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>`__
Expand Down
23 changes: 16 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,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")
Expand All @@ -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
}
20 changes: 14 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,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")
Expand All @@ -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
}
Empty file.
Loading