| package main |
|
|
| import ( |
| "context" |
| "encoding/json" |
| "fmt" |
| "log" |
| "os" |
| "time" |
|
|
| "github.com/joho/godotenv" |
| "go.mongodb.org/mongo-driver/bson" |
| "go.mongodb.org/mongo-driver/mongo" |
| "go.mongodb.org/mongo-driver/mongo/options" |
| ) |
|
|
| func main() { |
| godotenv.Load(".env") |
| uri := os.Getenv("MONGODB_URI") |
| if uri == "" { |
| log.Fatal("MONGODB_URI not set") |
| } |
|
|
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| defer cancel() |
|
|
| client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri)) |
| if err != nil { |
| log.Fatal(err) |
| } |
| defer client.Disconnect(ctx) |
|
|
| coll := client.Database("systemDesign").Collection("System_Design_Full_notes") |
|
|
| var result bson.M |
| err = coll.FindOne(ctx, bson.M{"chapter_no": 10}).Decode(&result) |
| if err != nil { |
| log.Fatal(err) |
| } |
|
|
| b, _ := json.MarshalIndent(result, "", " ") |
| fmt.Println(string(b)) |
| } |
|
|