如何在Go中重用MongoDB连接

dpiehjr4  于 2023-08-01  发布在  Go
关注(0)|答案(3)|浏览(130)

我想将我用Go编写的服务器与MongoDB连接,但我不确定如何以有效的方式完成。我发现的几个例子实现了它,如下所示。

libs/mongodb/client.go

package mongodb

import (
    "context"
    "log"
    "project/keys"

    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func GetClient() *mongo.Database {
    client, err := mongo.Connect(
        context.Background(),
        options.Client().ApplyURI(keys.GetKeys().MONGO_URI),
    )

    if err != nil {
        log.Fatal(err)
    }

    return client.Database(keys.GetKeys().MONGO_DB_NAME)
}

字符串

services/user/findOne.go

package userservices

import (
    "context"
    "log"
    "project/libs/mongodb"
    "project/models"

    "go.mongodb.org/mongo-driver/bson"
)

func FindOne(filter bson.M) (models.User, error) {
    var user models.User

    collection := mongodb.GetClient().Collection("users")
    result := collection.FindOne(context.TODO(), filter)

    if result.Err() != nil {
        return user, result.Err()
    }

    if err := result.Decode(&user); err != nil {
        log.Println("Failed to decode user with error:", err)
        return user, err
    }

    return user, nil
}


GetClient函数返回一个数据库示例,然后在整个应用程序中使用该示例。这似乎是可行的,但我想知道这是否真的是最佳实践,因为它似乎每次请求新客户端时都会创建一个新连接,如第二个代码片段所示,或者这种假设是不正确的?我还考虑过将GetClient转换为单例,它总是返回相同的数据库示例,但在这种情况下如何处理丢失的连接?谢谢你的好意

igsr9ssn

igsr9ssn1#

我这样做。在服务启动时执行一次,然后将MongoDatastore对象传递给编排器、服务层和存储库层。我正在使用mongo的“github.com/mongodb/mongo-go-driver/mongo”驱动程序。我认为它内部监控和回收空闲连接。因此,只要对mongo.Client对象的引用没有丢失,我们就不必担心连接断开。

const CONNECTED = "Successfully connected to database: %v"

type MongoDatastore struct {
    db      *mongo.Database
    Session *mongo.Client
    logger  *logrus.Logger
}

func NewDatastore(config config.GeneralConfig, logger *logrus.Logger) *MongoDatastore {

    var mongoDataStore *MongoDatastore
    db, session := connect(config, logger)
    if db != nil && session != nil {

        // log statements here as well

        mongoDataStore = new(MongoDatastore)
        mongoDataStore.db = db
        mongoDataStore.logger = logger
        mongoDataStore.Session = session
        return mongoDataStore
    }

    logger.Fatalf("Failed to connect to database: %v", config.DatabaseName)

    return nil
}

func connect(generalConfig config.GeneralConfig, logger *logrus.Logger) (a *mongo.Database, b *mongo.Client) {
    var connectOnce sync.Once
    var db *mongo.Database
    var session *mongo.Client
    connectOnce.Do(func() {
        db, session = connectToMongo(generalConfig, logger)
    })

    return db, session
}

func connectToMongo(generalConfig config.GeneralConfig, logger *logrus.Logger) (a *mongo.Database, b *mongo.Client) {

    var err error
    session, err := mongo.NewClient(generalConfig.DatabaseHost)
    if err != nil {
        logger.Fatal(err)
    }
    session.Connect(context.TODO())
    if err != nil {
        logger.Fatal(err)
    }

    var DB = session.Database(generalConfig.DatabaseName)
    logger.Info(CONNECTED, generalConfig.DatabaseName)

    return DB, session
}

字符串
您现在可以创建您的存储库如下:-

type TestRepository interface{
    Find(ctx context.Context, filters interface{}) []Document, error
}

type testRepository struct {
    store      *datastore.MongoDatastore
}

func (r *testRepository) Find(ctx context.Context , filters interface{}) []Document, error{
    cur, err := r.store.GetCollection("some_collection_name").Find(ctx, filters)
    if err != nil {
        return nil, err
    }
    defer cur.Close(ctx)
    var result = make([]models.Document, 0)
    for cur.Next(ctx) {
        var currDoc models.Document
        err := cur.Decode(&currDoc)
        if err != nil {
            //log here
            continue
        }
        result = append(result, currDoc)
    }
    return result, err
}

mo49yndu

mo49yndu2#

我用这个解决了

var CNX = Connection()
func Connection() *mongo.Client {
    // Set client options
    clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")

    // Connect to MongoDB
    client, err := mongo.Connect(context.TODO(), clientOptions)

    if err != nil {
        log.Fatal(err)
    }

    // Check the connection
    err = client.Ping(context.TODO(), nil)

    if err != nil {
    log.Fatal(err)
    }

    fmt.Println("Connected to MongoDB!")

    return client
  }

//calll connection
 func main() {
      collection := db.CNX.Database("tasks").Collection("task")
 }

output "Connected to MongoDB!"

字符串

8i9zcol2

8i9zcol23#

我所做的是这样的:
我在创建的config文件夹中有一个app.go文件,用于建立mongoDB连接

func Connection() *mongo.Client {
if err := godotenv.Load(); err != nil {
    log.Println("No .env file found")
}
uri := os.Getenv("MONGODB_URI")
if uri == "" {
    log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}
// Set client options
clientOptions := options.Client().ApplyURI(uri)

// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)

if err != nil {
    log.Fatal(err)
}

// Check the connection
err = client.Ping(context.TODO(), nil)

if err != nil {
    log.Fatal(err)
}

fmt.Println("Connected to MongoDB!")

return client
}

字符串
我在另一个名为employee.go的文件中调用了这个函数,该文件位于model文件夹

var CNX = config.Connection()

func (e *Employee) CreateEmployeeDetails() *Employee {
    coll := CNX.Database("employee").Collection("detail")
    fmt.Println(coll)
    result, err := coll.InsertOne(context.TODO(), e)
    fmt.Printf("Inserted document with _id: %v\n", result.InsertedID)
    if err != nil {
        panic(err)
    }

    return e
}

相关问题