【发布时间】:2012-07-12 06:07:41
【问题描述】:
我在 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 等的新手。
【问题讨论】: