I have an embedded document within a Mongodb document. The Mongodb document looks like this:
[_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
)
)
I’m trying to iterate over the people embedded document. In my app.rb, I’m passing the object to haml like this:
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
In my index.haml, I’m can iterate over the entire document like so:
-items.each do |item|
%h2= item
But how can I just iterate over the people embedded document? I’d like to do something like this:
-items.people.each do |person|
%h2= person.name, person.rewrite
Also, please correct me if I’m using improper terminology; I’m new to Mongo, Haml, Sinatra, etc.
Maybe I should learn Ruby syntax a bit more… Here’s how I was able iterate into my
peopleembedded document: