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
39 changes: 39 additions & 0 deletions source/includes/usage-examples/code-snippets/deleteOne.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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() {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))

if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()

// begin deleteOne
coll := client.Database("sample_mflix").Collection("movies")
result, err := coll.DeleteOne(context.TODO(), bson.D{{"title", "Twilight"}})
// end deleteOne

if err != nil {
panic(err)
}

// When you run this file for the first time, it should print "Documents deleted: 1"
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
}
2 changes: 1 addition & 1 deletion source/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Usage Examples

/usage-examples/find-operations
/usage-examples/update-operations
/usage-examples/delete-operations

..
/usage-examples/insert-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 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/usage-examples/run-example-tip.rst

.. literalinclude:: /includes/usage-examples/code-snippets/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 running the preceding code snippet, you should not be able to find
the following document 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
~~~~~~~~~~~~~~~~~

`DeleteOne() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.DeleteOne>`__
4 changes: 2 additions & 2 deletions source/usage-examples/find.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ The following example passes a query filter to the ``Find()`` method,
which matches documents in the ``zips`` collection where the value of the
``pop`` field is less than or equal to *500*:

.. include:: /includes/run-example-tip.rst
.. include:: /includes/usage-examples/run-example-tip.rst

.. literalinclude:: /code-snippets/find.go
.. literalinclude:: /includes/usage-examples/code-snippets/find.go
:start-after: begin find
:end-before: end find
:emphasize-lines: 4
Expand Down
6 changes: 3 additions & 3 deletions source/usage-examples/updateMany.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ The following example passes a query filter and an update parameter to the
field is "Sydney" and multiplies the ``price`` field in the matching
documents by a factor of *1.15*:

.. include:: /includes/run-example-tip.rst
.. include:: /includes/usage-examples/run-example-tip.rst

.. literalinclude:: /code-snippets/updateMany.go
.. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
:start-after: begin updatemany
:end-before: end updatemany
:emphasize-lines: 5
Expand Down Expand Up @@ -55,4 +55,4 @@ see the :manual:`MongoDB update operator reference documentation
API Documentation
~~~~~~~~~~~~~~~~~

`UpdateMany() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateMany>`_
`UpdateMany() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateMany>`__
6 changes: 3 additions & 3 deletions source/usage-examples/updateOne.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ The following example passes a query filter and an update parameter to the
by the value of its ``_id`` field and creates a new field in that document called
``avg_rating`` with a value of *4.4*:

.. include:: /includes/run-example-tip.rst
.. include:: /includes/usage-examples/run-example-tip.rst

.. literalinclude:: /code-snippets/updateOne.go
.. literalinclude:: /includes/usage-examples/code-snippets/updateOne.go
:start-after: begin updateone
:end-before: end updateone
:emphasize-lines: 6
Expand Down Expand Up @@ -55,4 +55,4 @@ see the :manual:`MongoDB update operator reference documentation
API Documentation
~~~~~~~~~~~~~~~~~

`UpdateOne() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateOne>`_
`UpdateOne() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.UpdateOne>`__