elixir-mongo / mongodb

MongoDB driver for Elixir
Apache License 2.0
568 stars 157 forks source link

Support multiple mongo repos #366

Closed clanchun closed 2 years ago

clanchun commented 2 years ago

Currently, only one mongo repo can be supported, due to the child id is hardcoded to Mongo, see https://github.com/elixir-mongo/mongodb/blob/master/lib/mongo.ex#L143-L150. If we have multiple repos in one application, we'll get the following error:

** (Mix) Could not start application content: Content.Application.start(:normal, []) returned an error: bad child specification, more than one child specification has the id: Mongo.
If using maps as child specifications, make sure the :id keys are unique.
If using a module or {module, arg} as child, use Supervisor.child_spec/2 to change the :id, for example:

    children = [
      Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
      Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)

Interestingly, this was actually supported before, but the behavior was changed in https://github.com/elixir-mongo/mongodb/commit/efc4df444d255258d2a68c00efd52c594e7cb786, I'm not sure what was the intention there, but I guess there's no problem to add it back, if so I'll be happy to create a PR.

ankhers commented 2 years ago

I'm assuming this was an accident. I think the simplest solution would be to just use

Enum.into(child_opts, %{ id: __MODULE__, start: {__MODULE__, :start_link, [opts]} })