Skip to content

Commit a04ad3b

Browse files
committed
update deleteMany example
1 parent 276fe5d commit a04ad3b

File tree

3 files changed

+118
-9
lines changed

3 files changed

+118
-9
lines changed

source/crud/delete.txt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,49 @@ existing document in the ``restaurants`` collection. Select the
173173

174174
Documents deleted: 1
175175

176-
Additional Information
177-
----------------------
176+
DeleteMany() Example: Full File
177+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178+
179+
.. include:: /includes/usage-examples/example-intro.rst
180+
181+
The following example is a fully runnable file that finds and deletes multiple
182+
existing documents in the ``restaurants`` collection. Select the
183+
:guilabel:`Struct` or :guilabel:`bson.D` tab to see the corresponding code:
184+
185+
.. tabs::
186+
187+
.. tab:: Struct
188+
:tabid: structExample
189+
190+
The following code uses structs to define and delete documents in the
191+
``restaurants`` collection:
192+
193+
.. input:: /includes/usage-examples/code-snippets/deleteMany.go
194+
:language: go
195+
:dedent:
196+
197+
.. output::
198+
:language: none
199+
:visible: false
200+
201+
Documents deleted: 2
202+
203+
.. tab:: BSON
204+
:tabid: bsonExample
205+
206+
The following code uses BSON documents to define and delete documents in the
207+
``restaurants`` collection:
208+
209+
.. input:: /includes/usage-examples/code-snippets/deleteManyBson.go
210+
:language: go
211+
:dedent:
212+
213+
.. output::
214+
:language: none
215+
:visible: false
216+
217+
Documents deleted: 2
178218

179-
For runnable examples of the delete operations, see the following usage
180219
examples:
181220

182221
- :ref:`golang-delete-one`

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ 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+
24+
// Creates a filter struct to specify the documents to delete
25+
type DeleteRestaurantFilter struct {
26+
Borough string `bson:"borough"`
27+
Cuisine string `bson:"cuisine"`
28+
}
29+
1630
func main() {
1731
if err := godotenv.Load(); err != nil {
1832
log.Println("No .env file found")
@@ -33,20 +47,21 @@ func main() {
3347
}
3448
}()
3549

36-
// begin deleteMany
37-
coll := client.Database("sample_mflix").Collection("movies")
38-
filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}}
50+
coll := client.Database("sample_restaurants").Collection("restaurants")
51+
filter := DeleteRestaurantFilter{
52+
Borough: "Queens",
53+
Cuisine: "German",
54+
}
3955

4056
// Deletes all documents that have a "runtime" value greater than 800
4157
results, err := coll.DeleteMany(context.TODO(), filter)
4258
if err != nil {
4359
panic(err)
4460
}
45-
// end deleteMany
4661

4762
// Prints the number of deleted documents
4863
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
4964

50-
// When you run this file for the first time, it should print:
51-
// Documents deleted: 4
65+
// When you run this file for the first time, it prints output similar to the following:
66+
// Documents deleted: 6
5267
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Deletes multiple documents from a collection by using the Go driver
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
"github.com/joho/godotenv"
11+
"go.mongodb.org/mongo-driver/v2/bson"
12+
"go.mongodb.org/mongo-driver/v2/mongo"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
14+
)
15+
16+
func main() {
17+
if err := godotenv.Load(); err != nil {
18+
log.Println("No .env file found")
19+
}
20+
21+
var uri string
22+
if uri = os.Getenv("MONGODB_URI"); uri == "" {
23+
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable")
24+
}
25+
26+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
27+
if err != nil {
28+
panic(err)
29+
}
30+
defer func() {
31+
if err = client.Disconnect(context.TODO()); err != nil {
32+
panic(err)
33+
}
34+
}()
35+
36+
// begin deleteMany
37+
coll := client.Database("sample_restaurants").Collection("restaurants")
38+
filter := bson.D{
39+
{"borough", "Queens"},
40+
{"cuisine", "German"},
41+
}
42+
43+
// Deletes all documents that have a "runtime" value greater than 800
44+
results, err := coll.DeleteMany(context.TODO(), filter)
45+
if err != nil {
46+
panic(err)
47+
}
48+
// end deleteMany
49+
50+
// Prints the number of deleted documents
51+
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
52+
53+
// When you run this file for the first time, it prints output similar to the following:
54+
// Documents deleted: 6
55+
}

0 commit comments

Comments
 (0)