Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/mongo_ecto/conversions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,25 @@ defmodule Mongo.Ecto.Conversions do

defp document(doc, pk) do
map(doc, fn {key, value} ->
pair(key, value, pk, &from_ecto_pk(&1, pk))
# Recurse with `nil` so the parent schema's primary key isn't
# accidentally rewritten when it appears inside a nested document
# (e.g. an `embeds_one` whose embedded schema has its own `:id`
# field). Without this guard, an embed like
#
# %{jurisdiction: %{id: "MD", title: "Maryland"}}
#
# gets serialized as `jurisdiction._id`, which breaks queries that
# filter on `jurisdiction.id`. The pk → _id remap is only meaningful
# at the top of the document.
pair(key, value, pk, &from_ecto_pk(&1, nil))
end)
Comment thread
scottmessinger marked this conversation as resolved.
end

defp document(doc, params, pk) do
map(doc, fn {key, value} ->
pair(key, value, pk, &inject_params(&1, params, pk))
# Same rationale as `document/2`: scope the pk → _id rename to the
# outer document. See the comment in `document/2`.
pair(key, value, pk, &inject_params(&1, params, nil))
end)
end

Expand Down
Loading