-
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
Open
stephmarie17
wants to merge
6
commits into
mongodb:comp-cov
Choose a base branch
from
stephmarie17:docsp-51816-insertUsageExamples
base: comp-cov
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ec8f22
move usag examples and add bsond
stephmarie17 afa26b3
fix formatting
stephmarie17 5e800e1
add expected output
stephmarie17 bd6295a
fix path
stephmarie17 f993919
lm feedback
stephmarie17 10b3dfe
standardize code comments with other pages
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
67 changes: 67 additions & 0 deletions
67
source/includes/usage-examples/code-snippets/insertManyBsonD.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,67 @@ | ||
// 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) | ||
} | ||
|
||
// When you run this file for the first time, it should print output similar | ||
// to the following: | ||
// 2 documents inserted with IDs: | ||
// ObjectID("...") | ||
// ObjectID("...") | ||
} |
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
54 changes: 54 additions & 0 deletions
54
source/includes/usage-examples/code-snippets/insertOneBsonD.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,54 @@ | ||
// 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) | ||
|
||
// When you run this file for the first time, it should print output similar | ||
// to the following: | ||
// Document inserted with ID: ObjectID("...") | ||
} |
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.