Search code examples
mongodbelixirerlang-supervisor

Start 2 MongoDB connections in Elixir


I am making an application that needs to connect to 2 mongo databases at the same time.

I am using the mongodb (v0.4) module.

For now my supervisor looks like that:

children=[
    worker(Mongo, [[name: :mongo, database: "transit", seeds: ["localhost:27017"], pool: DBConnection.Pool]])
]
opts= [strategy: :one_for_one, name: HugoEtl.Supervisor]
Supervisor.start_link(children, opts) 

I would like to open another connection at the same time to pump data from one connection to the other.

How can I achieve that.


Solution

  • What may prevent one from opening 2 connections in parallel naively would be the automatic Ids of workers corresponding to the equivalent module name.

    children=[
        worker(Mongo, [[name: :mongo, database: "transit", seeds: ["localhost:27017"], pool: DBConnection.Pool]]), 
        worker(Mongo, [[name: :mongo_final, database: "final", seeds: ["localhost:27017"], pool: DBConnection.Pool]], id: MongoFinal)
    ]
    opts= [strategy: :one_for_one, name: HugoEtl.Supervisor]
    Supervisor.start_link(children, opts) 
    

    This will start 2 database connections, you can query each with:

    # first connection 
    Mongo.find(:mongo,"collection",%{})
    # second connection 
    Mongo.find(:mongo_final,"collection",%{})