使用Ruby(1.8.7)和Mongo Driver,我写了这个:
@data['Users'] = []
@database['user_facilities'].find({ :facility_id => @facility['id'] }).each do |user_facility|
@data['Users'] << @database['users'].find({ :id => user_facility['user_id'] }).to_a
end
哪个输出:
[ "Users", [ [ { "name" => "michael" }, { "name" => "dennis" } ], [ { "name" => "brandon" }, { "name" => "steve" } ] ] ]
但我希望它看起来像这样:
[ "Users", [ { "name" => "michael" }, { "name" => "dennis" }, { "name" => "brandon" }, { "name" => "steve" } ] ]
我理解为什么我会得到上面的结果。我试过“扁平化”和“融合”。但似乎没有什么能像我想要的那样返回数据。
答案 0 :(得分:1)
@database['user_facilities'].find({ :facility_id => @facility['id'] }).each do |user_facility|
@data['Users'].push *@database['users'].find({ :id => user_facility['user_id'] }).to_a
end