Skip to content

Commit 70a6d18

Browse files
DOCSP-13842 crud delete page (#59)
* added CRUD Delete page
1 parent c644312 commit 70a6d18

File tree

5 files changed

+215
-8
lines changed

5 files changed

+215
-8
lines changed

source/fundamentals/crud/write-operations.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ Write Operations
55
.. default-domain:: mongodb
66

77
- :doc:`/fundamentals/crud/write-operations/insert`
8+
- :doc:`/fundamentals/crud/write-operations/delete`
89
- :doc:`/fundamentals/crud/write-operations/change-a-document`
910

1011
..
1112
- :doc:`/fundamentals/crud/write-operations/upsert`
12-
- :doc:`/fundamentals/crud/write-operations/delete`
1313
- :doc:`/fundamentals/crud/write-operations/embedded-arrays`
1414

1515
.. toctree::
1616
:caption: Write Operations
1717

1818
/fundamentals/crud/write-operations/insert
19+
/fundamentals/crud/write-operations/delete
1920
/fundamentals/crud/write-operations/change-a-document
2021

2122
..
2223
/fundamentals/crud/write-operations/upsert
23-
/fundamentals/crud/write-operations/delete
2424
/fundamentals/crud/write-operations/embedded-arrays
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
=================
2+
Delete a Document
3+
=================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. _delete_guide_golang:
14+
15+
Overview
16+
--------
17+
18+
In this guide, you can learn how to remove documents from your MongoDB
19+
collections using delete operations.
20+
21+
Sample Data
22+
~~~~~~~~~~~
23+
24+
To run the example in this guide, load these documents into the
25+
``ratings`` collection of the ``tea`` database with the following
26+
snippet:
27+
28+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go
29+
:language: go
30+
:dedent:
31+
:start-after: begin insertDocs
32+
:end-before: end insertDocs
33+
34+
.. tip:: Non-existent Database and Collections
35+
36+
The driver automatically creates the necessary database and/or collection
37+
when you perform a write operation against them if they don't already
38+
exist.
39+
40+
Each document contains a rating for a type of tea, which corresponds to
41+
the ``type`` and ``rating`` fields.
42+
43+
Delete Operations
44+
-----------------
45+
46+
Use **delete operations** to remove data from MongoDB. Delete operations
47+
consist of the following functions:
48+
49+
- ``DeleteOne()``, which deletes *the first document* that matches the filter
50+
- ``DeleteMany()``, which deletes *all* documents that match the filter
51+
52+
.. tip::
53+
54+
If one document matches your filter when running the ``DeleteMany()``
55+
function, it's equivalent to running the ``DeleteOne()`` function.
56+
57+
Parameters
58+
~~~~~~~~~~
59+
60+
The ``DeleteOne()`` and ``DeleteMany()`` functions expect you to pass a
61+
``Context`` type and a ``non-nil`` query filter specifying which
62+
documents to match.
63+
64+
They both optionally take a ``DeleteOptions`` type as a third parameter,
65+
which represents options you can use to configure the delete operation.
66+
If you don't specify a ``DeleteOptions``, the driver uses the default
67+
values for each option.
68+
69+
The ``DeleteOptions`` type allows you to configure options with the
70+
following functions:
71+
72+
.. list-table::
73+
:widths: 30 70
74+
:header-rows: 1
75+
76+
* - Function
77+
- Description
78+
79+
* - ``SetHint()``
80+
- | The index to use to scan for documents to delete.
81+
| Default: ``nil``
82+
83+
* - ``SetCollation()``
84+
- | The type of language collation to use when sorting results.
85+
| Default: ``nil``
86+
87+
Return Value
88+
~~~~~~~~~~~~
89+
90+
The ``DeleteOne()`` and ``DeleteMany()`` functions return a
91+
``DeleteResult`` type. This type contains the ``DeletedCount`` property,
92+
which states the number of documents deleted. If there are no matches to
93+
your filter, no document gets deleted and ``DeletedCount`` is ``0``.
94+
95+
Example
96+
```````
97+
98+
The following example performs the following with the ``DeleteMany()``
99+
function:
100+
101+
- Matches and deletes documents where the ``rating`` is greater than ``8``
102+
- Specifies the function to use the ``_id`` as the index
103+
104+
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go
105+
:language: go
106+
:dedent:
107+
:start-after: begin deleteMany
108+
:end-before: end deleteMany
109+
110+
After running the preceding example, the output resembles the following:
111+
112+
.. code-block:: none
113+
:copyable: false
114+
115+
Number of documents deleted: 2
116+
117+
.. tip::
118+
119+
If the preceding example used the ``DeleteOne()`` function instead of
120+
``DeleteMany()``, the driver would delete the first of the two
121+
matched documents.
122+
123+
Additional Information
124+
----------------------
125+
126+
For runnable examples of the delete operations, see the following usage
127+
examples:
128+
129+
- :doc:`Delete a Document </usage-examples/deleteOne>`
130+
- :doc:`Delete Multiple Documents </usage-examples/deleteMany>`
131+
132+
For more information about how the driver uses Context, see our guide on
133+
:doc:`Context </fundamentals/context>`.
134+
135+
.. For more information about specifying hints, see our guide on <TODO: Indexes>.
136+
.. For more information about collations, see our guide on <TODO: Collations>.
137+
138+
API Documentation
139+
~~~~~~~~~~~~~~~~~
140+
141+
For more information on any of the functions or types discussed in this
142+
guide, see the following API Documentation:
143+
144+
- `DeleteOne() <{+api+}/mongo#Collection.DeleteOne>`__
145+
- `DeleteMany() <{+api+}/mongo#Collection.DeleteMany>`__
146+
- `DeleteOptions <{+api+}/options#DeleteOptions>`__
147+
- `DeleteResult <{+api+}/mongo#DeleteResult>`__
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"go.mongodb.org/mongo-driver/bson"
10+
"go.mongodb.org/mongo-driver/mongo"
11+
"go.mongodb.org/mongo-driver/mongo/options"
12+
)
13+
14+
func main() {
15+
var uri string
16+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
17+
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://docs.mongodb.com/drivers/go/current/usage-examples/")
18+
}
19+
20+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
21+
22+
if err != nil {
23+
panic(err)
24+
}
25+
defer func() {
26+
if err = client.Disconnect(context.TODO()); err != nil {
27+
panic(err)
28+
}
29+
}()
30+
31+
client.Database("tea").Collection("ratings").Drop(context.TODO())
32+
33+
// begin insertDocs
34+
coll := client.Database("tea").Collection("ratings")
35+
docs := []interface{}{
36+
bson.D{{"type", "Masala"}, {"rating", 10}},
37+
bson.D{{"type", "Earl Grey"}, {"rating", 7}},
38+
bson.D{{"type", "Oolong"}, {"rating", 10}},
39+
bson.D{{"type", "Assam"}, {"rating", 7}},
40+
}
41+
42+
result, insertErr := coll.InsertMany(context.TODO(), docs)
43+
if insertErr != nil {
44+
panic(insertErr)
45+
}
46+
// end insertDocs
47+
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))
48+
49+
// begin deleteMany
50+
deleteManyFilter := bson.D{{"rating", bson.D{{"$gt", 8}}}}
51+
deleteOptions := options.Delete().SetHint(bson.D{{"_id", 1}})
52+
53+
deleteManyResult, deleteManyErr := coll.DeleteMany(context.TODO(), deleteManyFilter, deleteOptions)
54+
fmt.Printf("Number of documents deleted: %d\n", deleteManyResult.DeletedCount)
55+
// end deleteMany
56+
if deleteManyErr != nil {
57+
panic(deleteManyErr)
58+
}
59+
}

source/usage-examples/deleteMany.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ documents matched:
2323
:language: go
2424
:dedent:
2525

26-
View a `fully runnable example <{+example+}/deleteMany.go>`__
26+
View a `fully runnable example. <{+example+}/deleteMany.go>`__
2727

2828
Expected Result
2929
---------------
@@ -46,6 +46,9 @@ Multiple Documents Usage Example </usage-examples/find>`.
4646
Additional Information
4747
----------------------
4848

49+
For more information on deleting documents, see our guide on
50+
:ref:`deleting documents <delete_guide_golang>`.
51+
4952
API Documentation
5053
~~~~~~~~~~~~~~~~~
5154

source/usage-examples/deleteOne.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ matched:
2323
:language: go
2424
:dedent:
2525

26-
View a `fully runnable example <{+example+}/deleteOne.go>`__
26+
View a `fully runnable example. <{+example+}/deleteOne.go>`__
2727

2828
Expected Result
2929
---------------
@@ -43,10 +43,8 @@ a Document Usage Example </usage-examples/findOne>`.
4343
Additional Information
4444
----------------------
4545

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>.
46+
For more information on deleting documents, see our guide on
47+
:ref:`deleting documents <delete_guide_golang>`.
5048

5149
API Documentation
5250
~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)