mongodb_ecto icon indicating copy to clipboard operation
mongodb_ecto copied to clipboard

Query distinct values of a field

Open tmattio opened this issue 7 years ago • 4 comments
trafficstars

I am trying to query the distinct values of a field in all the documents as in: https://docs.mongodb.com/manual/reference/method/db.collection.distinct/

I tried to use the distinct function provided by Ecto (https://hexdocs.pm/ecto/Ecto.Query.html#distinct/3), but I have the following error:

** (Ecto.QueryError) MongoDB adapter does not support distinct clause in this query in query

Here is the code I try to run:

  def list_tags do
    query =
      from(
        a in Dataset,
        distinct: :tags
      )

    query
    |> Repo.all()
  end

If the distinct function is not supported by the adapter, is there any other way to get the distinct values of a field?

Thanks!

tmattio avatar Feb 02 '18 17:02 tmattio

Can you please tell me what version of this adapter you are currently using?

ankhers avatar Feb 02 '18 18:02 ankhers

I am using the branch ecto-2.1 of this repo:

{:mongodb_ecto, github: "michalmuskala/mongodb_ecto", branch: "ecto-2.1"},

And I have configured the adapter to be Mongo.Ecto.

tmattio avatar Feb 02 '18 18:02 tmattio

It looks like we are explicitly disallowing distinct here. I would have to see if we could allow for using the mongo distinct function from this.

ankhers avatar Feb 02 '18 20:02 ankhers

Thanks you!

I am new to Elixir, but I'll try to look into this as well.

tmattio avatar Feb 02 '18 21:02 tmattio