我正在使用 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"},
]}]
期望的结果是返回具有以"12345"结尾的"profile_url"链接的行。使用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
我认为这将做你想要的:
r.table('profiles').filter {|row|
row['groups'].contains {|group|
group['profile_url'].match('12345$')
}
}