Skip to content

Commit a0d7d6c

Browse files
DOCSP-13862 delete one usage example (#6)
* added usage example deleteOne page
1 parent 5016d29 commit a0d7d6c

File tree

10 files changed

+95
-9
lines changed

10 files changed

+95
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"go.mongodb.org/mongo-driver/bson"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
11+
)
12+
13+
// Replace the uri string with your MongoDB deployment's connection string.
14+
const uri = "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
15+
16+
func main() {
17+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
18+
19+
if err != nil {
20+
panic(err)
21+
}
22+
defer func() {
23+
if err = client.Disconnect(context.TODO()); err != nil {
24+
panic(err)
25+
}
26+
}()
27+
28+
// begin deleteOne
29+
coll := client.Database("sample_mflix").Collection("movies")
30+
result, err := coll.DeleteOne(context.TODO(), bson.D{{"title", "Twilight"}})
31+
// end deleteOne
32+
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
// When you run this file for the first time, it should print "Documents deleted: 1"
38+
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
39+
}

source/usage-examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Usage Examples
88

99
/usage-examples/find-operations
1010
/usage-examples/update-operations
11+
/usage-examples/delete-operations
1112

1213
..
1314
/usage-examples/insert-operations
14-
/usage-examples/delete-operations
1515
/usage-examples/bulkWrite
1616
/usage-examples/watch
1717
/usage-examples/count

source/usage-examples/deleteOne.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,50 @@ Delete a Document
33
=================
44

55
.. default-domain:: mongodb
6+
7+
You can delete a document in a collection using the ``DeleteOne()``
8+
method.
9+
10+
The following example specifies a query filter that matches documents in
11+
the ``movies`` collection with the value "Twilight" in the ``title`` field, and
12+
deletes the first document that matches.
13+
14+
.. include:: /includes/usage-examples/run-example-tip.rst
15+
16+
.. literalinclude:: /includes/usage-examples/code-snippets/deleteOne.go
17+
:start-after: begin deleteOne
18+
:end-before: end deleteOne
19+
:emphasize-lines: 2
20+
:language: go
21+
:dedent:
22+
23+
Click here <TODO> to see a fully runnable example.
24+
25+
Expected Result
26+
---------------
27+
28+
After running the preceding code snippet, you should not be able to find
29+
the following document in the ``movies`` collection:
30+
31+
.. code-block:: json
32+
33+
{ "_id": { "$oid": "573a13bff29313caabd5e06b" },
34+
...
35+
"title": "Twilight",
36+
...
37+
}
38+
39+
For an example on how to find a document, see our :doc:`Find
40+
One Usage Example </usage-examples/findOne>`.
41+
42+
Additional Information
43+
----------------------
44+
45+
For more information on deleting documents, specifying query filters,
46+
and handling potential errors, see our guide on <TODO:
47+
Deleting a Document>.
48+
49+
API Documentation
50+
~~~~~~~~~~~~~~~~~
51+
52+
`DeleteOne() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.DeleteOne>`__

source/usage-examples/find.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ The following example passes a query filter to the ``Find()`` method,
1111
which matches documents in the ``zips`` collection where the value of the
1212
``pop`` field is less than or equal to *500*:
1313

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

16-
.. literalinclude:: /code-snippets/find.go
16+
.. literalinclude:: /includes/usage-examples/code-snippets/find.go
1717
:start-after: begin find
1818
:end-before: end find
1919
:emphasize-lines: 4

source/usage-examples/updateMany.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ The following example passes a query filter and an update parameter to the
1313
field is "Sydney" and multiplies the ``price`` field in the matching
1414
documents by a factor of *1.15*:
1515

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

18-
.. literalinclude:: /code-snippets/updateMany.go
18+
.. literalinclude:: /includes/usage-examples/code-snippets/updateMany.go
1919
:start-after: begin updatemany
2020
:end-before: end updatemany
2121
:emphasize-lines: 5
@@ -55,4 +55,4 @@ see the :manual:`MongoDB update operator reference documentation
5555
API Documentation
5656
~~~~~~~~~~~~~~~~~
5757

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

source/usage-examples/updateOne.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ The following example passes a query filter and an update parameter to the
1212
by the value of its ``_id`` field and creates a new field in that document called
1313
``avg_rating`` with a value of *4.4*:
1414

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

17-
.. literalinclude:: /code-snippets/updateOne.go
17+
.. literalinclude:: /includes/usage-examples/code-snippets/updateOne.go
1818
:start-after: begin updateone
1919
:end-before: end updateone
2020
:emphasize-lines: 6
@@ -55,4 +55,4 @@ see the :manual:`MongoDB update operator reference documentation
5555
API Documentation
5656
~~~~~~~~~~~~~~~~~
5757

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

0 commit comments

Comments
 (0)