Kamva/mgm

Multi connection database

forderation opened this issue · 3 comments

Is your feature request related to a problem? Please describe.
I was confused about how to set mgm in multi-connection database from different configuration

Describe the solution you'd like
I tried before using mgm.NewClient to get multiple clients but am always got error timeout nil

Describe alternatives you've considered
Or from mgm please set default connection timeout from internal so it can be easy to config multi-client

Additional context
Multi client connection

Hi @forderation
Could you provide an example code that cause the error please?

If you want to select another DB or client for a collection, an example could be something like this:

func (model *Book) Collection() *mgm.Collection {
    // Get default connection client
  // client, err:= connectToMySecondMongoServer() // Use another client
   _, client, _, err := mgm.DefaultConfigs() // default client but different database

   if err != nil {
      panic(err)
   }

   db := client.Database("another_db") // 
   return mgm.NewCollection(db, "my_collection")
}

Hi @mehran-prs thanks for the answer
problem solved my client can connect but I need to config default to mgm like this:
_ = mgm.SetDefaultConfig(nil, "", options.Client().ApplyURI(""))
but why do we need to declare this on the main program? instead of using the default internal from the library. i thing it was better set default by internally and another user can override of configuration

You're welcome @forderation :)

but why do we need to declare this on the main program? instead of using the default internal from the library.

Because it's about the data and I think it would be better to do not modify user's data using a default connection(maybe we connect to a wrong database accidentally)