...

Source file src/go.mongodb.org/mongo-driver/cmd/testentauth/main.go

Documentation: go.mongodb.org/mongo-driver/cmd/testentauth

     1  // Copyright (C) MongoDB, Inc. 2022-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package main
     8  
     9  import (
    10  	"context"
    11  	"log"
    12  	"os"
    13  	"time"
    14  
    15  	"go.mongodb.org/mongo-driver/mongo"
    16  	"go.mongodb.org/mongo-driver/mongo/options"
    17  	"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
    18  )
    19  
    20  func main() {
    21  	uri := os.Getenv("MONGODB_URI")
    22  	compressor := os.Getenv("MONGO_GO_DRIVER_COMPRESSOR")
    23  
    24  	client, err := mongo.Connect(
    25  		context.Background(),
    26  		options.Client().ApplyURI(uri).SetCompressors([]string{compressor}))
    27  	if err != nil {
    28  		log.Fatalf("Error connecting client: %v", err)
    29  	}
    30  
    31  	// Use the defaultauthdb (i.e. the database name after the "/") specified in the connection
    32  	// string to run the count operation.
    33  	cs, err := connstring.Parse(uri)
    34  	if err != nil {
    35  		log.Fatalf("Error parsing connection string: %v", err)
    36  	}
    37  	if cs.Database == "" {
    38  		log.Fatal("Connection string must contain a defaultauthdb.")
    39  	}
    40  
    41  	coll := client.Database(cs.Database).Collection("test")
    42  
    43  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    44  	defer cancel()
    45  
    46  	count, err := coll.EstimatedDocumentCount(ctx)
    47  	if err != nil {
    48  		log.Fatalf("failed executing count command: %v", err)
    49  	}
    50  	log.Println("Count of test collection:", count)
    51  }
    52  

View as plain text