Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to query on embed field? #104

Open
AlexKovalevych opened this issue Jan 12, 2017 · 2 comments
Open

How to query on embed field? #104

AlexKovalevych opened this issue Jan 12, 2017 · 2 comments
Labels

Comments

@AlexKovalevych
Copy link
Contributor

AlexKovalevych commented Jan 12, 2017

Having such schema:

# ProjectUser module

schema "project_users" do
    belongs_to :project, Project

    embeds_many :stats, ProjectUserStat, on_replace: :delete
end
# ProjectUserStat module

  embedded_schema do
    field :date, :string
  end

Tried

      ProjectUser
      |> where([pu], pu.project_id in ^project_ids)
      |> where([pu], pu.stats.date >= ^from)

doesnt work:

** (Ecto.Query.CompileError) `pu.stats().date()` is not a valid query expression

tried with fragments:

      ProjectUser
      |> where([pu], pu.project_id in ^project_ids)
      |> where([pu], fragment("stats.date": ["$gte": ^from, "$lte": ^to]))

doesnt work:

[["stats.date": ["$gte": #BSON.ObjectId<5874d284958c27e7ccd906c3>, "$lte": #BSON.ObjectId<5874d284958c27e7ccd906c4>]]

Instead of having strings it converts them to ObjectIds. I'm using ecto 2.0 with this fork #91, so this might be related to that version only, anyway, any ideas?

@AlexKovalevych
Copy link
Contributor Author

After debugging, the solution is to use fragment first and in query after, so this one works:

      ProjectUser
      |> where([pu], fragment("stats.date": ["$gte": ^from, "$lte": ^to]))
      |> where([pu], pu.project_id in ^project_ids)

For some reason it uses wrong arguments position if i do it opposite.

@michalmuskala
Copy link
Collaborator

The ordering is definitely a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants