我在Mongodb文档中有一个嵌入式文档。 Mongodb文档如下所示:
[_id] => home
[url] => /
[type] => homepage
[people] => Array (
[0] => Array (
[name] => John Smith
[rewrite] => john-smith
)
[1] => Array (
[name] => John Q. Public
[rewrite] => john-q-public
)
)
我正在尝试迭代people
嵌入式文档。在我的app.rb中,我将对象传递给haml,如下所示:
DB = Mongo::Connection.new.db("website", :pool_size => 5, :timeout => 5)
pages = DB.collection('pages')
get '/' do
home = pages.find_one( :type => "homepage" )
haml :index, :attr_wrapper => '"', :locals => {:items => home}
end
在我的index.haml中,我可以像这样迭代整个文档:
-items.each do |item|
%h2= item
但是,如何迭代people
嵌入文档呢?我想做这样的事情:
-items.people.each do |person|
%h2= person.name, person.rewrite
另外,如果我使用不正确的术语,请纠正我;我是Mongo,Haml,Sinatra等的新手。
答案 0 :(得分:0)
也许我应该更多地学习Ruby语法......这就是我如何能够迭代到我的people
嵌入式文档中:
-items["people"].each do |person|
%h2= person["name"], person["rewrite"]