-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-51816 Move and standardize insert usage examples #538
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
7ec8f22
afa26b3
5e800e1
bd6295a
f993919
10b3dfe
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Inserts sample documents describing restaurants by using the Go driver with bson.D | ||
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") | ||
|
||
// Creates two sample documents describing restaurants using bson.D | ||
newRestaurants := []interface{}{ | ||
bson.D{ | ||
bson.E{Key: "name", Value: "Rule of Thirds"}, | ||
bson.E{Key: "cuisine", Value: "Japanese"}, | ||
}, | ||
bson.D{ | ||
bson.E{Key: "name", Value: "Madame Vo"}, | ||
bson.E{Key: "cuisine", Value: "Vietnamese"}, | ||
}, | ||
} | ||
|
||
// Inserts sample documents into the collection | ||
result, err := coll.InsertMany(context.TODO(), newRestaurants) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Prints the IDs of the inserted documents | ||
fmt.Printf("%d documents inserted with IDs:\n", len(result.InsertedIDs)) | ||
for _, id := range result.InsertedIDs { | ||
fmt.Printf("\t%s\n", id) | ||
} | ||
|
||
} |
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. S: I think we might still need the Restaurant data structure for the bson examples to aid the example. Not sure if we need to have it in BSON. I would ask the tech reviewer what they think. 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. Sounds good, will check with them! 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. @prestonvasquez when you have a moment to do tech review, can you take a look at this feedback from Lindsey? Should the bson examples also include the Restaurant data structure? Thank you! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Inserts a single document describing a restaurant by using the Go driver with bson.D | ||
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) | ||
} | ||
}() | ||
|
||
// Inserts a sample document describing a restaurant into the collection using bson.D | ||
coll := client.Database("sample_restaurants").Collection("restaurants") | ||
newRestaurant := bson.D{ | ||
bson.E{Key: "name", Value: "8282"}, | ||
bson.E{Key: "cuisine", Value: "Korean"}, | ||
} | ||
|
||
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) | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.