-
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
Merged
stephmarie17
merged 9 commits into
mongodb:comp-cov
from
stephmarie17:docsp-51819-standardizeDeleteUsageEx
Jul 28, 2025
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
276fe5d
update deleteOne usage example
stephmarie17 a04ad3b
update deleteMany example
stephmarie17 f34e8bd
update intro
stephmarie17 fbe3d9f
remove links to usage examples and fix formatting
stephmarie17 d72f411
Merge branch 'comp-cov' into docsp-51819-standardizeDeleteUsageEx
stephmarie17 97e567f
fix formatting
stephmarie17 4d127ed
update code comments
stephmarie17 8d7212e
js feedback
stephmarie17 c81b8ff
tech feedback
stephmarie17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
53 changes: 53 additions & 0 deletions
53
source/includes/usage-examples/code-snippets/deleteManyBson.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
source/includes/usage-examples/code-snippets/deleteOneBson.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.