| 1234567891011121314151617181920212223242526272829303132333435 | 
							- package mo
 
- import (
 
- 	"context"
 
- 	
 
- 	"go.mongodb.org/mongo-driver/mongo"
 
- 	"go.mongodb.org/mongo-driver/mongo/options"
 
- 	"go.mongodb.org/mongo-driver/mongo/readpref"
 
- )
 
- func NewClient(uri string) (*Client, error) {
 
- 	client, err := mongo.NewClient(options.Client().ApplyURI(uri))
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	ctx, cancel := context.WithTimeout(context.Background(), DefaultTimout)
 
- 	defer cancel()
 
- 	if err = client.Connect(ctx); err != nil {
 
- 		return nil, err
 
- 	}
 
- 	return client, client.Ping(ctx, readpref.Primary())
 
- }
 
- func NewClientWithAuth(uri string, auth Credential) (*Client, error) {
 
- 	client, err := mongo.NewClient(options.Client().ApplyURI(uri).SetAuth(auth))
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	ctx, cancel := context.WithTimeout(context.Background(), DefaultTimout)
 
- 	defer cancel()
 
- 	if err = client.Connect(ctx); err != nil {
 
- 		return nil, err
 
- 	}
 
- 	return client, client.Ping(ctx, readpref.Primary())
 
- }
 
 
  |