Skip to content

DOCSP-13862 delete one usage example #6

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
41 changes: 41 additions & 0 deletions source/includes/quick-start/deleteOne.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"
"log"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

// Replace the uri string with your MongoDB deployment's connection string.
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"

func main() {

ctx := context.TODO()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}()

// begin deleteOne
coll := client.Database("sample_mflix").Collection("movies")

result, err := coll.DeleteOne(ctx, bson.D{{"title", "The Room"}})
// end deleteOne

if err != nil {
log.Panic(err)
}

fmt.Printf("%v document was deleted.\n", result.DeletedCount)
// After running this file, it should print "1 document was deleted."
}
4 changes: 4 additions & 0 deletions source/includes/quick-start/run-example-tip.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. tip::

Read the :doc:`Usage Examples guide </usage-examples>` to learn how
to run this example.
3 changes: 1 addition & 2 deletions source/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ Usage Examples
.. toctree::

/usage-examples/find-operations

/usage-examples/delete-operations
..
/usage-examples/insert-operations
/usage-examples/update-operations
/usage-examples/delete-operations
/usage-examples/bulkWrite
/usage-examples/watch
/usage-examples/count
Expand Down
35 changes: 35 additions & 0 deletions source/usage-examples/deleteOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,38 @@ Delete a Document
=================

.. default-domain:: mongodb

You can delete a single document in a collection using the
``DeleteOne()`` method.

The following example specifies a query filter that matches documents in
the ``movies`` collection with the value "The Room" in the ``title`` field, and
deletes the first document that matches.

.. include:: /includes/quick-start/run-example-tip.rst

.. literalinclude:: /includes/quick-start/deleteOne.go
:start-after: begin deleteOne
:end-before: end deleteOne
:emphasize-lines: 3
:language: go
:dedent:

Click here <TODO> to see a fully runnable example.

Expected Result
---------------

After you run the code example, you cannot find the deleted document.

Additional Information
----------------------

For more information on deleting documents, specifying query filters,
and handling potential errors, see our guide on <TODO:
fundamental delete a doc>.

API Documentation
~~~~~~~~~~~~~~~~~

:go-api:`DeleteOne() <mongo#Collection.DeleteOne>`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar comment as on #5:
This link in on the staging server goes to pkg.go.dev documentation for the github.com/mongodb/mongo-go-driver package, but the canonical package is go.mongodb.org/mongo-driver. They're technically the same, but it could be confusing if the reader doesn't realize that.

Please update to link to the docs at https://pkg.go.dev/go.mongodb.org/mongo-driver instead.