Skip to content

Commit 0185666

Browse files
authored
DOCSP-51819 Standardize and move delete usage examples (#544)
1 parent c551dd8 commit 0185666

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

source/crud/delete.txt

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,54 @@ method:
136136
``DeleteMany()``, the driver would delete the first of the two
137137
matched documents.
138138

139-
Additional Information
140-
----------------------
139+
DeleteOne() Example: Full File
140+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141+
142+
.. include:: /includes/usage-examples/example-intro.rst
143+
144+
The following example is a fully runnable file that finds and deletes an
145+
existing document in the ``restaurants`` collection.
146+
147+
.. io-code-block::
148+
:copyable: true
149+
150+
.. input:: /includes/usage-examples/code-snippets/deleteOne.go
151+
:language: go
152+
:dedent:
153+
154+
.. output::
155+
:language: none
156+
:visible: false
157+
158+
Documents deleted: 1
159+
160+
DeleteMany() Example: Full File
161+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162+
163+
.. include:: /includes/usage-examples/example-intro.rst
164+
165+
The following example is a fully runnable file that performs the following
166+
actions on the ``restaurants`` collection:
141167

142-
For runnable examples of the delete operations, see the following usage
143-
examples:
168+
- Matches and deletes documents where the ``cuisine`` field value is ``German``
169+
and the ``borough`` field value is ``Queens``
170+
- Deletes all matching documents
144171

145-
- :ref:`golang-delete-one`
146-
- :ref:`golang-delete-many`
172+
.. io-code-block::
173+
:copyable: true
174+
175+
.. input:: /includes/usage-examples/code-snippets/deleteMany.go
176+
:language: go
177+
:dedent:
178+
179+
.. output::
180+
:language: none
181+
:visible: false
182+
183+
Documents deleted: 6
184+
185+
Additional Information
186+
----------------------
147187

148188
To learn more about performing the operations mentioned, see the
149189
following guides:
@@ -160,7 +200,7 @@ API Documentation
160200
~~~~~~~~~~~~~~~~~
161201

162202
To learn more about any of the methods or types discussed in this
163-
guide, see the following API Documentation:
203+
guide, see the following API documentation:
164204

165205
- `DeleteOne() <{+api+}/mongo#Collection.DeleteOne>`__
166206
- `DeleteMany() <{+api+}/mongo#Collection.DeleteMany>`__

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313
"go.mongodb.org/mongo-driver/v2/mongo/options"
1414
)
1515

16+
// Defines a Restaurant struct as a model for documents in the "restaurants" collection
17+
type Restaurant struct {
18+
ID bson.ObjectID `bson:"_id"`
19+
Name string `bson:"name"`
20+
Borough string `bson:"borough"`
21+
Cuisine string `bson:"cuisine"`
22+
}
23+
1624
func main() {
1725
if err := godotenv.Load(); err != nil {
1826
log.Println("No .env file found")
@@ -33,20 +41,21 @@ func main() {
3341
}
3442
}()
3543

36-
// begin deleteMany
37-
coll := client.Database("sample_mflix").Collection("movies")
38-
filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}}
44+
coll := client.Database("sample_restaurants").Collection("restaurants")
45+
filter := bson.D{
46+
{"borough", "Queens"},
47+
{"cuisine", "German"},
48+
}
3949

40-
// Deletes all documents that have a "runtime" value greater than 800
50+
// Deletes all documents that have a "Borough" value of "Queens" and a "Cuisine" value of "German"
4151
results, err := coll.DeleteMany(context.TODO(), filter)
4252
if err != nil {
4353
panic(err)
4454
}
45-
// end deleteMany
4655

4756
// Prints the number of deleted documents
4857
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
4958

50-
// When you run this file for the first time, it should print:
51-
// Documents deleted: 4
59+
// When you run this file for the first time, it prints output similar to the following:
60+
// Documents deleted: 6
5261
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import (
1313
"go.mongodb.org/mongo-driver/v2/mongo/options"
1414
)
1515

16+
// Defines a Restaurant struct as a model for documents in the "restaurants" collection
17+
type Restaurant struct {
18+
ID bson.ObjectID `bson:"_id"`
19+
Name string `bson:"name"`
20+
Cuisine string `bson:"cuisine,omitempty"`
21+
Address interface{} `bson:"address,omitempty"`
22+
Borough string `bson:"borough,omitempty"`
23+
Grades []interface{} `bson:"grades,omitempty"`
24+
}
25+
1626
func main() {
1727
if err := godotenv.Load(); err != nil {
1828
log.Println("No .env file found")
@@ -33,22 +43,20 @@ func main() {
3343
}
3444
}()
3545

36-
// begin deleteOne
37-
coll := client.Database("sample_mflix").Collection("movies")
38-
filter := bson.D{{"title", "Twilight"}}
46+
coll := client.Database("sample_restaurants").Collection("restaurants")
47+
filter := bson.D{{"name", "New Corner"}}
3948

40-
// Deletes the first document that has a "title" value of "Twilight"
49+
// Deletes the first document that has a "name" value of "New Corner"
4150
result, err := coll.DeleteOne(context.TODO(), filter)
4251

4352
// Prints a message if any errors occur during the operation
4453
if err != nil {
4554
panic(err)
4655
}
47-
// end deleteOne
4856

4957
// Prints the number of deleted documents
5058
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
5159

52-
// When you run this file for the first time, it should print:
60+
// When you run this file for the first time, it prints output similar to the following:
5361
// Documents deleted: 1
5462
}

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

Whitespace-only changes.

0 commit comments

Comments
 (0)