Trouble with inserting into MongoDB?

Hi all,

I’m a newbie with Elixir (Phoenix) so I’m having some trouble. Basically, I’m trying to create new user through API and get the token from Guardian package. I have setup mongo_db driver which should to my knowledge provide most of the functionality the normal Repo should. However, I’m unable to insert documents using the code below:

def create(conn, %{ "user" => user_params }) do   
    changeset = User.changeset(%User{}, user_params)

    case Repo.insert(changeset) do
      {:ok, user} ->
        new_conn = Guardian.Plug.api_sign_in(conn, user, :access)
        jwt = Guardian.Plug.current_token(new_conn)

        new_conn
        |> put_status(:created)
        |> render(TrackerApiWeb.SessionView, "show.json", user: user, jwt: jwt)
      {:error, changeset} ->
        conn
        |> put_status(:unprocessable_entity)
        |> render(TrackerApiWeb.ChangesetView, "error.json", changeset: changeset)
    end
end

Meanwhile, if I use the following I’m able to insert document but with some missing validation rules and again no token generated:

case Mongo.insert_one(:mongo, "users", changeset_new_user.changes, [return_document: :after, upsert: :true]) do
  {:ok, user} ->
    new_conn = Guardian.Plug.api_sign_in(conn, user, :access)
    jwt = Guardian.Plug.current_token(new_conn)

    new_conn
    |> put_status(:created)
    |> render(TrackerApiWeb.SessionView, "show.json", user: user, jwt: jwt)
  {:error, changeset} ->
    conn
    |> put_status(:unprocessable_entity)
    |> render(TrackerApiWeb.ChangesetView, "error.json", changeset: changeset)
end

I have been trying to debug the issue, google it but with no results. I do like Phoenix but compatibility and missing documentation especially on how to work with MongoDB it is making it harder to progress.

Here is also my mix.exs file:
def application do
[
mod: {TrackerApi.Application, []},
applications: [
:phoenix, :phoenix_pubsub, :gettext,
:phoenix_ecto, :mongodb, :mongodb_ecto, :ecto,
:cowboy, :logger,
:poolboy, :uuid,
:comeonin, :scrivener_ecto, :corsica
],
extra_applications: [:runtime_tools]
]
end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_),     do: ["lib"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      {:phoenix, "~> 1.2.1"},
      {:plug, "~>1.3.5", override: true},
      {:phoenix_pubsub, "~> 1.0"},
      {:phoenix_ecto, github: "phoenixframework/phoenix_ecto", ref: "v3.0.1"},
      {:mongodb, ">= 0.0.0"},
      {:mongodb_ecto, github: "zeroasterisk/mongodb_ecto", branch: "elixir-ecto-v2.0.1"},
      {:ecto, "~> 2.0"},
      {:gettext, "~> 0.11"},
      {:uuid, "~> 1.1" },
      {:timex, "~> 3.0"},
      {:cowboy, "~> 1.0"},
      {:comeonin, "~> 2.5"},
      {:guardian, "~> 0.13.0"},
      {:corsica, "~> 0.5.0"},
      {:scrivener_ecto, "~> 1.0"}
    ]
  end

Any help appreciated :slight_smile:

I have almost the same code… except this part

new_conn = Guardian.Plug.api_sign_in(conn, user, :access)
jwt = Guardian.Plug.current_token(new_conn)

Where I use

{:ok, jwt, _full_claims} = Guardian.encode_and_sign(user, :token)

I am using branch 0.14 of guardian on hex, but branch 1.0.0 is now available as beta, It might be soon time to check for the new syntax.

Hi @kokolegorille,

Thanks for your reply. However the problem it is happening before I come to that part, when inserting the changeset I get the error with the prompt
(UndefinedFunctionError) function Mongo.Ecto.insert/6 is undefined or private. Did you mean one of: * insert/5

I’m confused since I’m passing the changeset which is something like this:

#Ecto.Changeset<action: nil, changes: %{email: "test@test.com", encrypted_password: "$2b$12$FcwgZ9KfizP/X4g26YaBP.KpEmcVSSG8Ky7da8LSIiT4j3Af1cDLC", password: "test123"}, errors: [], data: #TrackerApiWeb.User<>, valid?: true>

Should I remove anything from the changeset to get the insert/5?

I am sorry, I use postgresql only and have no prior experience with mongodb :slight_smile:

1 Like

conversation on GitHub for anyone that may be having the same or similar problems.

Thanks, the issue is now solved, the dependencies were not correct.