如何为 Ecto 查询传递 JSON 中的值?

How to pass values in JSON for the Ecto Query?

客户端发送以下内容JSON:{"user":{"age+":18,"age-":40}}

在我的控制器中,我有以下内容:

def procura(conn, query) do
  maior = conn.params["user"]["age+"]
  menor = conn.params["user"]["age-"]
 query = from u in query, where: u.age > ^maior and u.age < ^menor, select: u.name
  pesquisa = Repo.all query
  IO.puts pesquisa
end

但是我收到了编译器警告:** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for %{}

如何为 Ecto 查询传递 JSON 中的值?

我的目标是基于 JSON 个字段进行查询。我想查询年龄在 x 到 y 之间的用户。 自动翻译。

您可能想要做的是:

在我的控制器中,我有以下内容:

def procura(conn, %{"user" => %{ "idade+" => maior, "idade-" => menor }}) do
  query = from u in Module.Model, where: u.idade > ^maior and u.idade < ^menor, select: u.name
  pesquisa = Repo.all query
  IO.puts pesquisa
  text conn, "Works"
end