mongo and or

This commit is contained in:
mr 2024-08-02 15:45:57 +02:00
parent 24a277333d
commit 09b67b26b0

View File

@ -62,30 +62,24 @@ func (m *MongoDB) createClient(MongoURL string) error {
defer cancel()
clientOptions := options.Client().ApplyURI(MongoURL).SetBSONOptions(bsonOpts)
mngoClient, err = mongo.Connect(MngoCtx, clientOptions)
if err != nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb NewClient " + MongoURL + ":" + err.Error())
}
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil || mngoClient == nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb connect " + MongoURL + ":" + err.Error())
}
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
if err = mngoClient.Ping(MngoCtx, nil); err != nil || mngoClient == nil {
mngoClient = nil
isConnected = false
return errors.New("Mongodb ping " + MongoURL + ":" + err.Error())
}
if !isConnected {
if !isConnected && mngoClient != nil {
m.Logger.Info().Msg("Connecting mongo client to db " + mngoConfig.GetDatabase())
m.prepareDB(mngoCollections, mngoConfig)
m.Logger.Info().Msg("Database is READY")
}
return nil
}