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
40 changes: 40 additions & 0 deletions source/includes/quick-start/deleteOne.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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", "Twilight"}})
// end deleteOne

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

// When you run this file for the first time, it should print "Number of documents deleted: 1"
fmt.Printf("Number of documents deleted: %d\n", result)
}
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
47 changes: 47 additions & 0 deletions source/usage-examples/deleteOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,50 @@ 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 "Twilight" 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: 2
:language: go
:dedent:

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

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

After deleting this document, you should not be able to find it in the
``movies`` collection:

.. code-block:: json

{ "_id": { "$oid": "573a13bff29313caabd5e06b" },
...
"title": "Twilight",
...
}

For an example on how to find a document, see our :doc:`Find
One Usage Example </usage-examples/findOne>`.

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

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

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.