Skip to content

Commit 5dfd5fa

Browse files
author
Mohammad Hunan Chughtai
authored
(DOCSP-7888): x509 go snippet (#578)
* (DOCSP-7887): added csharp x509 code * (DOCSP-7888): added x509 code for go * (DOCSP-7888): removed accidental push of csharp file * (DOCSP-7888): go snippet syntax improvements * (DOCSP-7888): added bson.D to countDocs * (DOCSP-7888): updated uri
1 parent 2df6709 commit 5dfd5fa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// begin x509 connection
2+
package main
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
"go.mongodb.org/mongo-driver/bson"
11+
)
12+
13+
func main() {
14+
ctx := context.TODO()
15+
certificateKeyFilePath := "/etc/certs/mongodb/client.pem"
16+
uri := "mongodb+srv://<cluster-url>/test?authSource=$external&tlsCertificateKeyFile=%s&retryWrites=true&w=majority&authMechanism=MONGODB-X509"
17+
uri = fmt.Sprintf(uri, certificateKeyFilePath)
18+
clientOpts := options.Client().ApplyURI(uri)
19+
20+
client, err := mongo.Connect(ctx, clientOpts)
21+
if err != nil { log.Fatal(err) }
22+
defer client.Disconnect(ctx)
23+
24+
collection := client.Database("testDB").Collection("testCol")
25+
docCount, err := collection.CountDocuments(ctx, bson.D{})
26+
if err != nil { log.Fatal(err) }
27+
fmt.Println(docCount)
28+
}
29+
// end x509 connection

0 commit comments

Comments
 (0)