fireproofsocks
Starting Mongo inside a mix Task
I need to run some commands on a Mongo database from within a custom mix task. I’m using the mongodb driver.
The task module looks like this:
defmodule Mix.Tasks.Mongo.Stuff do
use Mix.Task
@shortdoc "Does stuff to mongo..."
def run(_) do
{:ok, _pid} = Mongo.start_link(Application.get_env(:db, DocDb))
Mongo.find_one(:docdb, "the_collection", %{id: 456})
end
end
and the relevant bit of the config looks like this:
config :db, DocDb,
url: "mongodb://localhost:27017/docdb_dev",
name: :docdb
This seems to get Mongo started (?) but it generates a spooky error:
mix mongo.stuff
Compiling 1 file (.ex)
** (EXIT from #PID<0.92.0>) an exception was raised:
** (ArgumentError) argument error
:erlang.!(Mongo.Events, {:notify, %Mongo.Events.TopologyOpeningEvent{topology_pid: #PID<0.305.0>}})
(stdlib) gen_event.erl:269: :gen_event.send/2
lib/mongo/topology.ex:70: Mongo.Topology.init/1
(stdlib) gen_server.erl:374: :gen_server.init_it/2
(stdlib) gen_server.erl:342: :gen_server.init_it/6
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
I’m not even sure what I’m looking at. Can someone help set me straight? Thanks!
Most Liked
NobbZ
mix tasks don’t start your or any other applications. When you need some, you need to start them manually using Application.ensure_all_started/1.
3
Popular in Questions
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New







