-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-51818 Standardize replace usage ex #541
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 4 commits
ea067d5
a36a4b6
ac9ebdc
d0e5687
2a96116
6e218b6
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 |
---|---|---|
|
@@ -8,12 +8,10 @@ import ( | |
"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" | ||
) | ||
|
||
// start-restaurant-struct | ||
type Restaurant struct { | ||
Name string | ||
RestaurantId string `bson:"restaurant_id,omitempty"` | ||
|
@@ -23,7 +21,9 @@ type Restaurant struct { | |
Grades []interface{} `bson:"grades,omitempty"` | ||
} | ||
|
||
// end-restaurant-struct | ||
type RestaurantNameFilter struct { | ||
Name string | ||
} | ||
|
||
func main() { | ||
if err := godotenv.Load(); err != nil { | ||
|
@@ -45,23 +45,21 @@ func main() { | |
} | ||
}() | ||
|
||
// begin replace | ||
coll := client.Database("sample_restaurants").Collection("restaurants") | ||
filter := bson.D{{"name", "Madame Vo"}} | ||
filter := RestaurantNameFilter{Name: "Rizzo's Fine Pizza"} | ||
|
||
|
||
// Creates a new document containing "Name" and "Cuisine" fields | ||
replacement := Restaurant{Name: "Monsieur Vo", Cuisine: "Asian Fusion"} | ||
replacement := Restaurant{Name: "Rizzo's Pizza", Cuisine: "Pizza/American"} | ||
|
||
// Replaces the first document that matches the filter with a new document | ||
result, err := coll.ReplaceOne(context.TODO(), filter, replacement) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// end replace | ||
|
||
// Prints the number of modified documents | ||
if result.MatchedCount != 0 { | ||
fmt.Println("Number of documents replaced: %d\n", result.ModifiedCount) | ||
fmt.Println("Number of documents replaced:", result.ModifiedCount) | ||
} | ||
|
||
// When you run this file for the first time, it should print: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Replaces the first document that matches a filter 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", "Rizzo's Fine Pizza"}} | ||
|
||
// Creates a new document containing "Name" and "Cuisine" fields | ||
replacement := bson.D{ | ||
bson.E{Key: "name", Value: "Rizzo's Pizza"}, | ||
bson.E{Key: "cuisine", Value: "Pizza/American"}, | ||
} | ||
|
||
// Replaces the first document that matches the filter with a new document | ||
result, err := coll.ReplaceOne(context.TODO(), filter, replacement) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Prints the number of modified documents | ||
if result.MatchedCount != 0 { | ||
fmt.Println("Number of documents replaced:", result.ModifiedCount) | ||
} | ||
|
||
// When you run this file for the first time, it should print: | ||
|
||
// Number of documents replaced: 1 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a struct as a filter is quite awkward and error prone. For example, say you have the following filter struct:
In Go, if you don't populate both fields then the filter will use the "zero" value for Rating:
So if the actual rating is anything other than "0", nothing will be found since the underlying command is something of this shape:
Suggest retaining
bson.D
for the filter. Using a go struct for decoding results into is best practice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, let me check in with @rishitb-mongodb .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @prestonvasquez 's thought makes sense. Let's standardize on defining the queries via
bson.D
(i.e for any filters) and using the struct to represent the documents (when inserting new documents or retrieving results).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will do.