-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdelete.txt
167 lines (117 loc) · 4.53 KB
/
delete.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
.. _golang-delete-guide:
================
Delete Documents
================
.. meta::
:description: Learn how to remove documents from MongoDB collections using delete operations like `DeleteOne()` and `DeleteMany()`, with examples and options.
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
Overview
--------
In this guide, you can learn how to remove documents from your MongoDB
collections using delete operations.
Sample Data
~~~~~~~~~~~
The example in this guide uses the following ``Book`` struct as a model for documents
in the ``books`` collection:
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go
:start-after: start-book-struct
:end-before: end-book-struct
:language: go
:dedent:
To run the example in this guide, load the sample data into the
``db.books`` collection with the following snippet:
.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/delete.go
:language: go
:dedent:
:start-after: begin insertDocs
:end-before: end insertDocs
Each document contains a description of a book that
includes the title, author, and page length corresponding to
the ``title``, ``author``, and ``length`` fields in each document.
.. include:: /includes/fundamentals/automatic-db-coll-creation.rst
Delete Operations
-----------------
Use **delete operations** to remove data from MongoDB. Delete operations
consist of the following methods:
- ``DeleteOne()``, which deletes *the first document* that matches the filter
- ``DeleteMany()``, which deletes *all* documents that match the filter
.. tip::
If one document matches your filter when running the ``DeleteMany()``
method, it's equivalent to running the ``DeleteOne()`` method.
Parameters
~~~~~~~~~~
The ``DeleteOne()`` and ``DeleteMany()`` methods expect you to pass a
``Context`` type and a ``non-nil`` query filter specifying which
documents to match.
They both optionally take a ``DeleteOptions`` type as a third parameter,
which represents options you can use to configure the delete operation.
If you don't specify a ``DeleteOptions``, the driver uses the default
values for each option.
The ``DeleteOptions`` type allows you to configure options with the
following methods:
.. list-table::
:widths: 30 70
:header-rows: 1
* - Method
- Description
* - ``SetHint()``
- | The index to use to scan for documents to delete.
| Default: ``nil``
* - ``SetCollation()``
- | The type of language collation to use when sorting results.
| Default: ``nil``
Return Value
~~~~~~~~~~~~
The ``DeleteOne()`` and ``DeleteMany()`` methods return a
``DeleteResult`` type. This type contains the ``DeletedCount`` property,
which states the number of documents deleted. If there are no matches to
your filter, no document gets deleted and ``DeletedCount`` is ``0``.
Example
```````
The following example performs the following with the ``DeleteMany()``
method:
- Matches and deletes documents where the ``length`` is greater than ``300``
- Instructs the method to use the ``_id`` as the index
.. io-code-block::
:copyable: true
.. input::
:language: go
filter := bson.D{{"length", bson.D{{"$gt", 300}}}}
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
result, err := coll.DeleteMany(context.TODO(), filter, opts)
if err != nil {
panic(err)
}
fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)
.. output::
:language: none
:visible: false
Number of documents deleted: 2
.. tip::
If the preceding example used the ``DeleteOne()`` method instead of
``DeleteMany()``, the driver would delete the first of the two
matched documents.
Additional Information
----------------------
For runnable examples of the delete operations, see the following usage
examples:
- :ref:`golang-delete-one`
- :ref:`golang-delete-many`
To learn more about performing the operations mentioned, see the
following guides:
- :ref:`golang-query-document`
To learn about how the driver uses Context, see :ref:`golang-context`.
To learn more about specifying hints, see :ref:`golang-indexes`.
To learn more about collations, see :ref:`golang-collations`.
API Documentation
~~~~~~~~~~~~~~~~~
To learn more about any of the methods or types discussed in this
guide, see the following API Documentation:
- `DeleteOne() <{+api+}/mongo#Collection.DeleteOne>`__
- `DeleteMany() <{+api+}/mongo#Collection.DeleteMany>`__
- `DeleteOptions <{+api+}/mongo/options#DeleteOptions>`__
- `DeleteResult <{+api+}/mongo#DeleteResult>`__