-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-51825 Standardize Struct Tag Usage ex #546
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Specifies struct tags on a struct by using the Go driver | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
"go.mongodb.org/mongo-driver/v2/mongo" | ||
"go.mongodb.org/mongo-driver/v2/mongo/options" | ||
) | ||
|
||
// Specifies a different name for RestaurantID | ||
// and marks certain fields as omitempty | ||
type Restaurant struct { | ||
Name string | ||
RestaurantId string `bson:"restaurant_id,omitempty"` | ||
Cuisine string `bson:"cuisine,omitempty"` | ||
Address interface{} `bson:"address,omitempty"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can leave this for a future piece of work, as we would probably want to make this update in the rest of the Go docs. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Edit: Making a ticket for that change sounds like a great idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Borough string `bson:"borough,omitempty"` | ||
Grades []interface{} `bson:"grades,omitempty"` | ||
} | ||
|
||
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/usage-examples/#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") | ||
|
||
// Creates a Restaurant document | ||
|
||
newRestaurant := Restaurant{ | ||
Name: "Amazing Pizza", | ||
RestaurantId: "123456789", | ||
Cuisine: "American", | ||
} | ||
|
||
// Inserts the sample document describing a restaurant into the collection | ||
|
||
result, err := coll.InsertOne(context.TODO(), newRestaurant) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Prints the ID of the inserted document | ||
fmt.Printf("Document inserted with ID: %s\n", result.InsertedID) | ||
|
||
// When you run this file, it should print: | ||
// Document inserted with ID: ObjectID("...") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type Restaurant struct { | ||
Name string | ||
RestaurantId string `bson:"restaurant_id,omitempty"` | ||
Cuisine string `bson:"cuisine,omitempty"` | ||
Address interface{} `bson:"address,omitempty"` | ||
Borough string `bson:"borough,omitempty"` | ||
Grades []interface{} `bson:"grades,omitempty"` | ||
} |
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.
Is the BSON struct tag intentionally omitted on
Name
?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.
Yes, to show user that you don't have to include a struct tag if you're fine with how the field will be marshaled.