Skip to content

Commit 276fe5d

Browse files
committed
update deleteOne usage example
1 parent 61b2f4c commit 276fe5d

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed

source/crud/delete.txt

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

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. Select the
146+
:guilabel:`Struct` or :guilabel:`bson.D` tba to see the corresponding code:
147+
148+
.. tabs::
149+
150+
.. tab:: Struct
151+
:tabid: structExample
152+
153+
The following code uses structs to define and delete a document in the
154+
``restaurants`` collection:
155+
156+
.. input:: /includes/usage-examples/code-snippets/deleteOne.go
157+
:language: go
158+
:dedent:
159+
160+
.. output::
161+
:language: none
162+
:visible: false
163+
164+
Documents deleted: 1
165+
166+
.. input:: /includes/usage-examples/code-snippets/deleteOneBson.go
167+
:language: go
168+
:dedent:
169+
170+
.. output::
171+
:language: none
172+
:visible: false
173+
174+
Documents deleted: 1
175+
139176
Additional Information
140177
----------------------
141178

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ 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+
}
21+
22+
// Creates a filter struct to specify the document to delete
23+
type DeleteRestaurantFilter struct {
24+
Name string `bson:"name"`
25+
}
26+
1627
func main() {
1728
if err := godotenv.Load(); err != nil {
1829
log.Println("No .env file found")
@@ -33,18 +44,16 @@ func main() {
3344
}
3445
}()
3546

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

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

4353
// Prints a message if any errors occur during the operation
4454
if err != nil {
4555
panic(err)
4656
}
47-
// end deleteOne
4857

4958
// Prints the number of deleted documents
5059
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Deletes a document 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+
coll := client.Database("sample_restaurants").Collection("restaurants")
37+
filter := bson.D{{"name", "New Corner"}}
38+
39+
// Deletes the first document that has a "name" value of "New Corner"
40+
result, err := coll.DeleteOne(context.TODO(), filter)
41+
42+
// Prints a message if any errors occur during the operation
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
// Prints the number of deleted documents
48+
fmt.Printf("Documents deleted: %d\n", result.DeletedCount)
49+
50+
// When you run this file for the first time, it prints output similar to the following:
51+
// Documents deleted: 1
52+
}

0 commit comments

Comments
 (0)