在phoenix/elixir中使用mongodb驱动程序进行深度查询
我正在使用来自的mongodb驱动程序查询elixir/phoenix项目中的mongodb数据库。一个简单的查询,例如在phoenix/elixir中使用mongodb驱动程序进行深度查询,mongodb,elixir,phoenix,Mongodb,Elixir,Phoenix,我正在使用来自的mongodb驱动程序查询elixir/phoenix项目中的mongodb数据库。一个简单的查询,例如 cursor = Mongo.find(:mongo, "posts",%{}) list = Enum.to_list(cursor) object= Enum.fetch(list,0) object= elem(object, 1) new_list=Map.fetch(object, "name") new
cursor = Mongo.find(:mongo, "posts",%{})
list = Enum.to_list(cursor)
object= Enum.fetch(list,0)
object= elem(object, 1)
new_list=Map.fetch(object, "name")
new_list=elem(new_list,1)
new_cursor= Mongo.find(:mongo, "posts",%{"name" => new_list})
new_list=Enum.to_list(new_cursor)
没有问题,但我想知道如何执行更深入的搜索,因为我有嵌套的JSON,例如
{"posts":{"name":"something","attributes":{"aspect":{"and_so_on":"endpoint"}}}}.
那么,在这种情况下,如何到达“端点” 首先,您的代码非常不地道。不应该在每个步骤上重新分配值,我们通常使用管道 也就是说,您的原始游标可能会写为
列表=
:mongo
|>Mongo.find(“posts”,%{})
|>Enum.fetch(0)
|>要素(1)
|>Map.fetch(“名称”)
|>要素(1)
新名单=
:mongo
|>find(“posts”、%{“name”=>list})
|>枚举到_列表()
或者,更好的方法是使用模式匹配
%{“name”=>one}=
Mongo.find_one(:Mongo,“posts”,%{})
一
#⇒ “什么”
MongoDB还支持中的,如中所示。要访问嵌套元素,可以执行以下操作:
Mongo.find(:Mongo,“posts”,
%{“attributes”=>%{“aspect”=>“和”}}