Mongodb 如何在Phoenix中处理嵌入式模式?

Mongodb 如何在Phoenix中处理嵌入式模式?,mongodb,elixir,phoenix-framework,ecto,Mongodb,Elixir,Phoenix Framework,Ecto,假设我有userfollow这样的模型 schema "users" do field :user_name, :string field :password, :string end schema "address" do field :line1, :string field :country, :string end 和地址型号如下 schema "users" do field :user_name, :string field :password, :str

假设我有
user
follow这样的模型

schema "users" do
  field :user_name, :string
  field :password, :string
end
schema "address" do
  field :line1, :string
  field :country, :string
end
地址
型号如下

schema "users" do
  field :user_name, :string
  field :password, :string
end
schema "address" do
  field :line1, :string
  field :country, :string
end
我使用MongoDB作为数据库,所以我希望json格式如下

schema "users" do
  field :user_name, :string
  field :password, :string
end
schema "address" do
  field :line1, :string
  field :country, :string
end
{id:“dfd”,用户名:“$$$”,密码:“xxx”,地址:{line1:“line1”,国家:“印度”}

1) 如何创建和验证更改集,其中
用户
模式中的用户名和
地址
模式中的国家/地区是必填字段?
2) 如何在验证两者之后获得最终变更集

假设mongo适配器的工作方式与postgresql jsonb列类似:

defmodule User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "users" do
    field :user_name, :string
    field :password, :string
    embeds_one :address, Address
  end

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, [:user_name, :password]
    |> cast_embed(:address)
    |> validate_required(:user_name, :password, :address)
  end
end

defmodule Address do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  embedded_schema do 
    field :line1, :string
    field :country, :string
  end

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, [:line1, :country])
    |> validate_required([:line1, :country])
  end
end

changeset.changes
看起来如下
%{user\u name:“hari”,password:****,address:#exto.changeset}
但是当我尝试插入时,我得到了类似于#exto.changeset未实现协议枚举的错误。请尝试在插入之前将
addess
changeset转换为结构:cs=user.changeset(%user{},params)address=apply_change(get_change(cs,:address))cs=put_change(cs,:address,address)它为嵌入式地址文档创建
\u id
,也
{“\u id”:ObjectId(“58fda5c9421aa915beeb6ff4”),“address:{“国家”:“testcountry”,“id”:ObjectId(“58fda5c9421aa915beeb6ff3”),“line1:“testline1”,“用户名”:“检查”,“密码”:“******”}
我犯了什么错误?我的错误。忘记在嵌入式地址架构上禁用自动主键生成。答案已编辑。现在我收到类似于模式XXXX的错误。地址没有主键