-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-51819 Standardize and move delete usage examples #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
276fe5d
a04ad3b
f34e8bd
fbe3d9f
d72f411
97e567f
4d127ed
8d7212e
c81b8ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,20 @@ import ( | |
| "go.mongodb.org/mongo-driver/v2/mongo/options" | ||
| ) | ||
|
|
||
| // Defines a Restaurant struct as a model for documents in the "restaurants" collection | ||
| type Restaurant struct { | ||
| ID bson.ObjectID `bson:"_id"` | ||
| Name string `bson:"name"` | ||
| Borough string `bson:"borough"` | ||
| Cuisine string `bson:"cuisine"` | ||
| } | ||
|
|
||
| // Creates a filter struct to specify the documents to delete | ||
| type DeleteRestaurantFilter struct { | ||
|
||
| Borough string `bson:"borough"` | ||
| Cuisine string `bson:"cuisine"` | ||
| } | ||
|
|
||
| func main() { | ||
| if err := godotenv.Load(); err != nil { | ||
| log.Println("No .env file found") | ||
|
|
@@ -33,20 +47,21 @@ func main() { | |
| } | ||
| }() | ||
|
|
||
| // begin deleteMany | ||
| coll := client.Database("sample_mflix").Collection("movies") | ||
| filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}} | ||
| coll := client.Database("sample_restaurants").Collection("restaurants") | ||
| filter := DeleteRestaurantFilter{ | ||
| Borough: "Queens", | ||
| Cuisine: "German", | ||
| } | ||
|
|
||
| // Deletes all documents that have a "runtime" value greater than 800 | ||
| // Deletes all documents that have a "Borough" value of "Queens" and a "Cuisine" value of "German" | ||
| results, err := coll.DeleteMany(context.TODO(), filter) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| // end deleteMany | ||
|
|
||
| // Prints the number of deleted documents | ||
| fmt.Printf("Documents deleted: %d\n", results.DeletedCount) | ||
|
|
||
| // When you run this file for the first time, it should print: | ||
| // Documents deleted: 4 | ||
| // When you run this file for the first time, it prints output similar to the following: | ||
| // Documents deleted: 6 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Deletes multiple documents from a collection by using the Go driver | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
||
| "github.com/joho/godotenv" | ||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| "go.mongodb.org/mongo-driver/v2/mongo" | ||
| "go.mongodb.org/mongo-driver/v2/mongo/options" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := godotenv.Load(); err != nil { | ||
| log.Println("No .env file found") | ||
| } | ||
|
|
||
| var uri string | ||
| if uri = os.Getenv("MONGODB_URI"); uri == "" { | ||
| 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") | ||
| } | ||
|
|
||
| client, err := mongo.Connect(options.Client().ApplyURI(uri)) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer func() { | ||
| if err = client.Disconnect(context.TODO()); err != nil { | ||
| panic(err) | ||
| } | ||
| }() | ||
|
|
||
| coll := client.Database("sample_restaurants").Collection("restaurants") | ||
| filter := bson.D{ | ||
| {"borough", "Queens"}, | ||
| {"cuisine", "German"}, | ||
| } | ||
|
|
||
| // Deletes all documents that have a "borough" value of "Queens" and a "cuisine" value of "German | ||
| results, err := coll.DeleteMany(context.TODO(), filter) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Prints the number of deleted documents | ||
| fmt.Printf("Documents deleted: %d\n", results.DeletedCount) | ||
|
|
||
| // When you run this file for the first time, it prints output similar to the following: | ||
| // Documents deleted: 6 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Deletes a document from a collection by using the Go driver | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
||
| "github.com/joho/godotenv" | ||
| "go.mongodb.org/mongo-driver/v2/bson" | ||
| "go.mongodb.org/mongo-driver/v2/mongo" | ||
| "go.mongodb.org/mongo-driver/v2/mongo/options" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := godotenv.Load(); err != nil { | ||
| log.Println("No .env file found") | ||
| } | ||
|
|
||
| var uri string | ||
| if uri = os.Getenv("MONGODB_URI"); uri == "" { | ||
| 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") | ||
| } | ||
|
|
||
| client, err := mongo.Connect(options.Client().ApplyURI(uri)) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer func() { | ||
| if err = client.Disconnect(context.TODO()); err != nil { | ||
| panic(err) | ||
| } | ||
| }() | ||
|
|
||
| coll := client.Database("sample_restaurants").Collection("restaurants") | ||
| filter := bson.D{{"name", "New Corner"}} | ||
|
|
||
| // Deletes the first document that has a "name" value of "New Corner" | ||
| result, err := coll.DeleteOne(context.TODO(), filter) | ||
|
|
||
| // Prints a message if any errors occur during the operation | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Prints the number of deleted documents | ||
| fmt.Printf("Documents deleted: %d\n", result.DeletedCount) | ||
|
|
||
| // When you run this file for the first time, it prints output similar to the following: | ||
| // Documents deleted: 1 | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.