我正在使用ruby驱动程序来重新思考db并且我遇到了包含数组的嵌套元素的一些问题。
我的数据结构如下
[{"family_name"=>"Adam"},
{"family_name"=>"Bobby"},
{"family_name"=>"Crissy",
"groups"=>
[{"logo_url"=>"https://www.logourl.com/1111.png",
"name"=>"Sample Name 1",
"profile_url"=>"https://www.profileurl.com/groups?gid=1111"},
{"logo_url"=>"https://www.logourl.com/2222.png",
"name"=>"Sample Name 2",
"profile_url"=>"https://www.profileurl.com/groups?gid=2222"},
{"logo_url"=>"https://www.logourl.com/3333.png",
"name"=>"Sample Name 3",
"profile_url"=>"https://www.profileurl.com/groups?gid=3333"},
]},
{"family_name"=>"Bobby"},
{"family_name"=>"Hillton",
"groups"=>
[{"logo_url"=>"https://www.logourl.com/4444.png",
"name"=>"Sample Name 1",
"profile_url"=>"https://www.profileurl.com/groups?gid=4444"},
{"logo_url"=>"https://www.logourl.com/5555.png",
"name"=>"Sample Name 2",
"profile_url"=>"https://www.profileurl.com/groups?gid=55555"},
{"logo_url"=>"https://www.logourl.com/6666.png",
"name"=>"Sample Name 3",
"profile_url"=>"https://www.profileurl.com/groups?gid=12345"},
]}]
所需的结果是返回具有' profile_url'的行。链接以" 12345"结尾。使用Ruby和Javascript的混合,我试过这个:
'r.table("profiles").filter{ |row| row['groups'].contains(r.js('function(group) { return group('profile_url').match("12345$") }'))}.run(conn)'
然而,它仍然没有返回结果,是否有一个明确的方法与ruby驱动程序一起使用???
期望的结果是
[{"family_name"=>"Hillton",
"groups"=>
[{"logo_url"=>"https://www.logourl.com/4444.png",
"name"=>"Sample Name 1",
"profile_url"=>"https://www.profileurl.com/groups?gid=4444"},
{"logo_url"=>"https://www.logourl.com/5555.png",
"name"=>"Sample Name 2",
"profile_url"=>"https://www.profileurl.com/groups?gid=55555"},
{"logo_url"=>"https://www.logourl.com/6666.png",
"name"=>"Sample Name 3",
"profile_url"=>"https://www.profileurl.com/groups?gid=12345"},
]}]
请提前帮助和谢谢! :d
答案 0 :(得分:3)
我认为这会做你想做的事情:
r.table('profiles').filter {|row|
row['groups'].contains {|group|
group['profile_url'].match('12345$')
}
}