Save data to mongodb through code

ecto
mongodb

#1

Hello.
I using Mongo.Ecto to mongodb connection, when I run in CLI commands:

person = %Friends.Person{}
Friends.Repo.insert(person)

I got result and a record is created. But with same commands in code I can’t do it. How do it?
Repository with full proj:


#2

Friend is a module… it should contains functions You can call.

In your code…

defmodule Friends do
    IO.puts "start"
    person = %Friends.Person{}
    Friends.Repo.insert(person)
    IO.puts "saved"
end

it could be

defmodule Friends do
  def save_a_new_person() do
    IO.puts "start"
    person = %Friends.Person{}
    Friends.Repo.insert(person)
    IO.puts "saved"
  end
end

And then You can call it like this

iex> Friends.save_a_new_person() 

Why would You expect your code in the module to run? or when?


#3

I don’t want do it through iex because through iex it’s work well.
It’s a test project, I need to do it in my development, but I can’t share a main project.(settings is same)


#4

If it is just to create some user, I would use the seed file instead. Still I don’t see when You expect this code to run.


#5

When I run mix data need to be saved.
In code with command .insted() I can’t save to db, got error:

== Compilation error in file lib/friends.ex ==
** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Mongo.IdServer, :machineprocid, 2)
    lib/mongo/id_server.ex:50: Mongo.IdServer.new/0
    lib/ecto/repo/schema.ex:651: Ecto.Repo.Schema.autogenerate_id/4
    lib/ecto/repo/schema.ex:199: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
    lib/friends.ex:4: (module)

#6

As @kokolegorille has already pointed out, you shouldn’t run db commands outside of functions, since then they would be executed during compilation before the database connection process is started.

Put those commands in a function, and execute the function when you need to. It can be in response to a controller action or whatever else.

Also maybe read through the official elixir guide to get a better understanding of how elixir modules and functions work.


#7

Okay, so I think u mean something like this:

defmodule Friends do
  def save() do
    IO.puts "start"
    person = %Friends.Person{}
    Friends.Repo.insert(person)
    IO.puts "saved"
  end
end

Friends.save()

Unfortunately when I try start it through mix mistake still the same:27


#8

Okay, so I think u mean something like this:

No … It would still try to invoke your function before starting the connection to the database. What exactly are you trying to do?


#9

I want to connect to the database before my function start


#10

Currently you are trying to save your data during build time of your application.

This requires you to set up your database connection during build time manually and is probably not the thing you want. Especially not if this does not make sense to you.

But without knowing your exact architecture I think its hard to tell you how to do it properly.

But I assume you are doing a phoenix application here, so just call the save from your controller.


#11

I doesn’t develop phoenix app.
My architecture is:
I build my project using a microservices architecture(rabbitmq), one of my worker need to be builded using elixir, when I get message from my queue, for example I want to save it to db, so how do it?


#12

I do not use AMQP in elixir, but I do assume, that you have to either poll for messages one by one or register a callback or have to implement a behaviour to receive your messages.

Regardless how you receive your messages, in the code that is executed on receive you need to do your stuff.


#13

I want to connect to the database before my function start

Then you can run this function as a Task after supervisor(Repo, []) in your application.
Replace https://github.com/dersar00/friends/blob/master/lib/friends/application.ex with

defmodule Friends.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    import Supervisor.Spec
    children = [
      supervisor(Friends.Repo, []),
      worker(Task, [&Friends.save/0], restart: :transient)
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Friends.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Don’t forget to delete https://github.com/dersar00/friends/blob/master/lib/friends.ex#L10

My architecture is:
I build my project using a microservices architecture(rabbitmq), one of my worker need to be builded using elixir, when I get message from my queue, for example I want to save it to db, so how do it?

You would call your function like Friends.save(data) from a handler which handles new messages from rabbitmq, just like a phoenix controller which handles new requests.

You wouldn’t need the worker(Task, [&Friends.save/0], restart: :transient) from above.