Skip to content

Commit 07832fa

Browse files
DOCSP-13863 delete many usage example (#11)
* added DeleteMany usage example page
1 parent a0d7d6c commit 07832fa

File tree

5 files changed

+103
-12
lines changed

5 files changed

+103
-12
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 deleteMany
29+
coll := client.Database("sample_mflix").Collection("movies")
30+
filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}}
31+
32+
results, err := coll.DeleteMany(context.TODO(), filter)
33+
// end deleteMany
34+
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
// When you run this file for the first time, it should print "Documents deleted: 4"
40+
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
41+
}

source/includes/usage-examples/code-snippets/deleteOne.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func main() {
2727

2828
// begin deleteOne
2929
coll := client.Database("sample_mflix").Collection("movies")
30-
result, err := coll.DeleteOne(context.TODO(), bson.D{{"title", "Twilight"}})
30+
filter := bson.D{{"title", "Twilight"}}
31+
32+
result, err := coll.DeleteOne(context.TODO(), filter)
3133
// end deleteOne
3234

3335
if err != nil {

source/usage-examples/deleteMany.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,52 @@ Delete Multiple Documents
33
=========================
44

55
.. default-domain:: mongodb
6+
7+
You can delete multiple documents in a collection by using the
8+
``DeleteMany()`` method.
9+
10+
The following example specifies a query filter to the ``DeleteMany()``
11+
method, which matches documents in the ``movies`` collection where the
12+
``runtime`` field is greater than *800* and deletes all the matching
13+
documents.
14+
15+
.. include:: /includes/usage-examples/run-example-tip.rst
16+
17+
.. literalinclude:: /includes/usage-examples/code-snippets/deleteMany.go
18+
:start-after: begin deleteMany
19+
:end-before: end deleteMany
20+
:emphasize-lines: 4
21+
:language: go
22+
:dedent:
23+
24+
Click here <TODO> to see a fully runnable example.
25+
26+
Expected Result
27+
---------------
28+
29+
After running the preceding code snippet, you should not be able to find
30+
the following documents in the ``movies`` collection:
31+
32+
.. code-block:: json
33+
:copyable: false
34+
35+
// results truncated
36+
{ "_id": ObjectId("573a1397f29313caabce69db"), ... , "runtime": 1256, ... },
37+
{ "_id": ObjectId("573a1397f29313caabce75fe"), ... , "runtime": 910, ... },
38+
{ "_id": ObjectId("573a1399f29313caabcee1aa"), ... , "runtime": 1140, ... },
39+
{ "_id": ObjectId("573a13a6f29313caabd18ae0"), ... , "runtime": 877, ... }
40+
41+
For an example on how to find multiple documents, see our :doc:`Find
42+
Usage Example </usage-examples/find>`.
43+
44+
Additional Information
45+
----------------------
46+
47+
For more information on deleting documents, specifying query filters,
48+
and handling potential errors, see our guide on <TODO:
49+
Deleting a Document>.
50+
51+
API Documentation
52+
~~~~~~~~~~~~~~~~~
53+
54+
`DeleteMany() <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#Collection.DeleteMany>`__

source/usage-examples/deleteOne.txt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ Delete a Document
77
You can delete a document in a collection using the ``DeleteOne()``
88
method.
99

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.
10+
The following example specifies a query filter to the ``DeleteOne()``
11+
method, which matches documents in the ``movies`` collection where the
12+
``title`` field is "Twilight", and deletes the first document that
13+
matches.
1314

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

1617
.. literalinclude:: /includes/usage-examples/code-snippets/deleteOne.go
1718
:start-after: begin deleteOne
1819
:end-before: end deleteOne
19-
:emphasize-lines: 2
20+
:emphasize-lines: 4
2021
:language: go
2122
:dedent:
2223

@@ -29,12 +30,10 @@ After running the preceding code snippet, you should not be able to find
2930
the following document in the ``movies`` collection:
3031

3132
.. code-block:: json
33+
:copyable: false
3234

33-
{ "_id": { "$oid": "573a13bff29313caabd5e06b" },
34-
...
35-
"title": "Twilight",
36-
...
37-
}
35+
// result truncated
36+
{ "_id": ObjectId("573a13bff29313caabd5e06b"), ..., "title": "Twilight", ... }
3837

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

source/usage-examples/find.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ see the :manual:`MongoDB query operator reference documentation
5252
API Documentation
5353
~~~~~~~~~~~~~~~~~
5454

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

0 commit comments

Comments
 (0)