elixir-mongo / elixir-mongo/mongodb_ecto
How to query on embed field?
- Dominant language
- Elixir
- Stars
- 378
- Forks
- 127
- PR merge metrics
- No merged PRs in 30d
Description
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 https://github.com/michalmuskala/mongodb_ecto/pull/91, so this might be related to that version only, anyway, any ideas?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.