Reputation: 12679
I'm looking for a better way, if it's possible, for manipulating an existing mondodb datamodel using amazing mongoid ODM driver.
Suppose you have an embedded one to many data model like the following :
class User
include Mongoid::Document
field :nickname
embeds_many :watchlists
end
class Watchlist
include Mongoid::Document
field :html_url
field :description
field :tags_array, type: Array
embedded_in :user
end
Now, for all users you want to extract just a part of every watchlist, if and only if, it has
tags_array == ["ruby", "web", "framework"]
having back just few fields (not the entire watchlist doc):
and
I tried something like this :
1.9.2p290 :574 > Competitor = Struct.new(:html_url, :description, :user)
=> #<Class:0xed08de8>
1.9.2p290 :575 > competitors = []
=> []
1.9.2p290 :576 > User.all.map do |user|
1.9.2p290 :577 > user.watchlists.all.map do |wl|
1.9.2p290 :578 > if wl.tags_array == ["ruby", "web", "framework"]
1.9.2p290 :579?> competitors << Competitor.new(wl.html_url, wl.description, wl.user.nickname)
1.9.2p290 :580?> end
1.9.2p290 :581?> end
1.9.2p290 :582?> end
here are some resoults :
1.9.2p290 :585 > competitors
=> [#<struct html_url="https://github.com/rails/rails", description="Ruby on Rails", user="lgs">, #<struct html_url="https://github.com/sinatra/sinatra", description="Classy web-development dressed in a DSL (official / canonical repo)", user="lgs">]
1.9.2p290 :586 > competitors.size
=> 2
1.9.2p290 :599 > competitors[0][:html_url]
=> "https://github.com/rails/rails"
1.9.2p290 :600 > competitors[1][:html_url]
=> "https://github.com/sinatra/sinatra"
1.9.2p290 :601 >
But I wonder, if there are better, smarter, faster, efficient, effective, aesthetic ( or just "different" ) way, of doing that ...
Upvotes: 1
Views: 287
Reputation: 10907
You do two things:
only fetch fields you need from db, rather than the whole user objects(assuming you have some other stuff in user, which you omitted here for brevity)
Competitor = Struct.new(:html_url, :description, :user)
competitors = []
User.where('watchlists.tags_array' => %w[ruby web framework]).
only(:nickname, :watchlists).each do |u|
u.watchlists.where(:tags_array => %w[ruby web framework]).each do |wl|
competitors << Competitor.new(wl.html_url, wl.description, u.nickname)
end
end
PS: Probably you do not want to use map
on User.all
, it will require a lot of memory if you have lots of heavy user documents. Also, you are not using the mapped users, but instead collecting results in the competitors
array yourself, so each
should work just fine.
Upvotes: 2