-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-51815 Move and standardize find usage exs #539
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 3 commits
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,64 @@ | ||
// Retrieves documents that match a query filter by using the Go driver | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"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 a query filter to match documents in which the "cuisine" | ||
// is "Italian" | ||
filter := bson.D{{"cuisine", "Italian"}} | ||
|
||
// Retrieves documents that match the query filter | ||
cursor, err := coll.Find(context.TODO(), filter) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Unpacks the cursor into a slice | ||
var results []Restaurant | ||
if err = cursor.All(context.TODO(), &results); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Prints the results of the find operation as structs | ||
for _, result := range results { | ||
cursor.Decode(&result) | ||
output, err := json.MarshalIndent(result, "", " ") | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("%s\n", output) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Retrieves a document that matches a query filter by using the Go driver | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"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 a query filter to match documents in which the "name" is | ||
// "Bagels N Buns" | ||
filter := bson.D{{"name", "Bagels N Buns"}} | ||
|
||
// Retrieves the first matching document | ||
var result Restaurant | ||
err = coll.FindOne(context.TODO(), filter).Decode(&result) | ||
|
||
// Prints a message if no documents are matched or if any | ||
// other errors occur during the operation | ||
if err != nil { | ||
if err == mongo.ErrNoDocuments { | ||
return | ||
} | ||
panic(err) | ||
} | ||
|
||
output, err := json.MarshalIndent(result, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("%s\n", output) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.